如何将外部Xaml页面链接到通用Windows平台中的网格?

Kar*_*sil 2 c# xaml win-universal-app windows-10-universal

我想在我的通用Windows平台应用程序中将外部.xaml文件链接到网格中.

这是文件夹结构:

在此输入图像描述

我想将ListView.xaml链接到一个在MainPage.xaml中声明的网格

两个文件的代码:

MainPage.xaml中:

<Page
x:Class="TodoGrocery.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TodoGrocery"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid x:Name="gridView" Grid.Row="0" HorizontalAlignment="Stretch"  Margin="0,0,0,0" VerticalAlignment="Top" Height="40" Background="#3A5194">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="50"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="backButton" BorderThickness="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent">
            <SymbolIcon  Symbol="Back" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Width="40" Height="40"/>
        </Button>
        <TextBlock x:Name="title" Grid.Column="1" HorizontalAlignment="Center" FontSize="20" VerticalAlignment="Center" Foreground="White" Text="Todo Grocery"></TextBlock>
        <Button x:Name="moreButton" BorderThickness="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent">
            <SymbolIcon Symbol="More" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Width="40" Height="40"/>
        </Button>
    </Grid>



    <Grid Grid.Row="1">
        <Page><!-- Link ListView.xaml Here--></Page>
    </Grid>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

ListView.xaml

<Page
x:Class="TodoGrocery.ListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TodoGrocery"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid Background="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="40"/>
    </Grid.RowDefinitions>
    <Grid x:Name="listHeader" Grid.Row="0" BorderBrush="#d0d0d0" BorderThickness="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="40"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="40"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <CheckBox Grid.Column="0" IsChecked="True"></CheckBox>
            <TextBlock Grid.Column="1" Text="Name" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,9,0,9"></TextBlock>
        </Grid>
        <Button Grid.Column="1">
            <SymbolIcon Symbol="Sort"/>
        </Button>
        <TextBlock Grid.Column="2" Text="Quantity" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,20,0"></TextBlock>
    </Grid>
    <Grid x:Name="ListPanel" Grid.Row="1"></Grid>
    <Grid x:Name="ButtonPanel" Grid.Row="2" Background="#3A5194">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="deleteAllButton" BorderThickness="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="White" Background="Transparent">
            <!--<SymbolIcon Symbol="Delete" Foreground="White"/>-->
            <StackPanel Orientation="Horizontal" Height="30">
                <SymbolIcon Symbol="Delete" Foreground="White"/>
                <TextBlock VerticalAlignment="Center" Foreground="White" Margin="10,0,0,0">Delete All</TextBlock>
            </StackPanel>
        </Button>
        <Button x:Name="addButton" Grid.Column="1" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="White" Background="Transparent">
            <!--<SymbolIcon Symbol="Delete" Foreground="White"/>-->
            <StackPanel Orientation="Horizontal" Height="30" Width="100">
                <SymbolIcon Symbol="Add" Foreground="white"/>
                <TextBlock VerticalAlignment="Top" Foreground="White" HorizontalAlignment="Right" Margin="15,5,0,0">Add</TextBlock>
            </StackPanel>
        </Button>
    </Grid>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)

所以这就是我打算用我的应用程序做的事情:

我将使用不同的外部页面,如ListView.xaml,每当用户单击链接或选项卡时,页面链接的部分应该与要求的页面一起更改.

谢谢.任何其他想法也赞赏.

Kev*_*sse 5

要将Page包含在另一个内,请使用该Frame对象:

<Grid Grid.Row="1">
    <Frame x:Name="MainFrame"></Frame>
</Grid>
Run Code Online (Sandbox Code Playgroud)

然后,要在Frame内部加载页面,只需调用Navigate方法:

this.MainFrame.Navigate(typeof(TodoGrocery.ListView));
Run Code Online (Sandbox Code Playgroud)