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

C# 如何在通用Windows平台中将外部Xaml页面链接到网格?,c#,xaml,win-universal-app,windows-10-universal,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.microsof

我想在我的通用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>

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>

全部删除
添加
这就是我计划对我的应用程序所做的:

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


谢谢。任何其他想法也值得赞赏。

要将一页包含在另一页中,请使用对象:


正如Kory所说,通过阅读Windows 10应用程序操作指南中的一章,您可以了解如何在UWP中实现自己的目标

这里的重点是使用和方法。框架承载页面并保存导航历史记录

您可以根据需要创建任意多个不同的页面类型来显示应用程序中的内容,然后通过调用navigate方法并传入要导航到的页面类型来导航到这些页面。您还可以传入参数对象以将页面初始化为特定状态

因此,您可以将MainPage.xaml更改为链接ListView.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:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:TodoGrocery"
      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">
            <Frame x:Name="MyFrame">
                <local:ListView />
            </Frame>
        </Grid>
    </Grid>
</Page>
在代码隐藏中,使用

MyFrame.Navigate(typeof(TodoGrocery.ListView));

对于UWP,请阅读。使用
UserControl
TemplatedControl
代替
Page
<Page x:Class="TodoGrocery.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:TodoGrocery"
      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">
            <Frame x:Name="MyFrame">
                <local:ListView />
            </Frame>
        </Grid>
    </Grid>
</Page>
<Grid Grid.Row="1">
    <Frame x:Name="MyFrame" />
</Grid>
MyFrame.Navigate(typeof(TodoGrocery.ListView));