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

时间:2016-03-07 02:03:15

标签: c# xaml win-universal-app windows-10-universal

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

这是文件夹结构:

enter image description here

我想将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,每当用户点击链接或标签时,页面链接的部分都应该根据要求的页面进行更改。

感谢。任何其他想法也受到赞赏。

2 个答案:

答案 0 :(得分:5)

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

<Grid Grid.Row="1">
    <Frame x:Name="MainFrame"></Frame>
</Grid>

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

this.MainFrame.Navigate(typeof(TodoGrocery.ListView));

答案 1 :(得分:1)

正如Kory所说,通过阅读Windows 10应用程序操作指南中的Navigation章节,您可以了解如何在UWP中执行所需操作。

这里的关键点是使用FrameFrame.Navigate方法。框架托管页面并保留导航历史记录。

  

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

因此,您可以将以下链接更改为MainView.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>

或者只需在网格中添加Frame

<Grid Grid.Row="1">
    <Frame x:Name="MyFrame" />
</Grid>

在代码隐藏中,使用

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