如何在XAML中创建主布局模板

如何在XAML中创建主布局模板,xaml,templates,layout,windows-10,uwp,Xaml,Templates,Layout,Windows 10,Uwp,我不熟悉windows开发。我正在制作一个简单的windows应用程序,它有几个页面,每个页面在XAML中都有类似的布局。像这样: 每一页分为3个部分。A将有一个标题,B是插入内容的位置,C是其他内容。我的问题是:构建通用布局模板的最简单方法是什么,这样我就可以在每个页面上重用它?有可能吗 例如,我有一个MainTemplate.xaml文件,其布局如下: <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBru

我不熟悉windows开发。我正在制作一个简单的windows应用程序,它有几个页面,每个页面在XAML中都有类似的布局。像这样:

每一页分为3个部分。A将有一个标题,B是插入内容的位置,C是其他内容。我的问题是:构建通用布局模板的最简单方法是什么,这样我就可以在每个页面上重用它?有可能吗

例如,我有一个MainTemplate.xaml文件,其布局如下:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
</Grid>


然后我的Page1.xaml将加载MainTemplate,这样我就不必将相同的布局复制并粘贴到我的每个页面中。我试着在网上查找,但解决方法让我不知所措。我想知道是否有一种简单的方法可以像对待网页一样做到这一点。非常感谢。

一种可行的方法是将
UserControl
ContentPresenter
一起使用。例如:

添加名为
main模板的
UserControl
。在XAML中,使用
ContentPresenter
设置布局,并将其绑定到code behind中定义的
DependencyProperty

<UserControl x:Class="UWPTest.MainTemplate"
             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:UWPTest"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="400"
             mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <ContentPresenter Content="{x:Bind Title}" />

        <ContentPresenter Grid.Row="1" Content="{x:Bind Main}" />

        <ContentPresenter Grid.Row="2" Content="{x:Bind Stuff}" />
    </Grid>
</UserControl>
在此之后,我们可以在其他页面中使用
UserControl
,以重用总体布局。例如,在“MainPage.xaml”中使用它:


A.
B
C
然后“主页”将如下所示:

不幸的是,有很多方法可以实现这一点,每种方法都有优点和缺点——例如,看看
Frame
s和
Page
s(因为它通常是UWP应用程序的基础)。或者您可以创建一个可以承载其他部分的自定义控件。如果没有XAML(使用C#代码),这将如何实现?
<Page x:Class="UWPTest.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:UWPTest"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <local:MainTemplate>
        <local:MainTemplate.Title>
            <Grid Background="Red">
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60">A</TextBlock>
            </Grid>
        </local:MainTemplate.Title>
        <local:MainTemplate.Main>
            <Grid Background="Green">
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60">B</TextBlock>
            </Grid>
        </local:MainTemplate.Main>
        <local:MainTemplate.Stuff>
            <Grid Background="Yellow">
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60">C</TextBlock>
            </Grid>
        </local:MainTemplate.Stuff>
    </local:MainTemplate>
</Page>