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

标签 xaml templates layout windows-10 uwp

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

enter image description here

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

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

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

然后我的 Page1.xaml 将加载 MainTemplate,因此我不必将相同的布局复制并粘贴到我的每个页面中。我试过在网上寻找,但解决方案超出了我的想象。我想知道是否有一种简单的方法可以像使用网页一样做到这一点。非常感谢。

最佳答案

一种可行的方法是使用 UserControlContentPresenter .例如:

添加 UserControl命名为 MainTemplate .在 XAML 中,使用 ContentPresenter 设置布局并将其绑定(bind)到 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>

在代码隐藏中,设置 DependencyProperty以便我们可以使用它们来设置其他页面中的内容。

public sealed partial class MainTemplate : UserControl
{
    public MainTemplate()
    {
        this.InitializeComponent();
    }

    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));

    public object Title
    {
        get { return GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty MainProperty = DependencyProperty.Register("Main", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));

    public object Main
    {
        get { return GetValue(MainProperty); }
        set { SetValue(MainProperty, value); }
    }

    public static readonly DependencyProperty StuffProperty = DependencyProperty.Register("Stuff", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));

    public object Stuff
    {
        get { return GetValue(StuffProperty); }
        set { SetValue(StuffProperty, value); }
    }
}

在此之后,我们可以使用 UserControl在其他页面中重用总体布局。例如,在“MainPage.xaml”中使用它:
<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>

然后“MainPage”将如下所示:
enter image description here

关于xaml - 如何在 XAML 中创建主布局模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34193938/

相关文章:

c# - 如何更新缓存页面中 UI 中的数据绑定(bind)元素?

c++ - 使用 sfinae 启用/禁用 typedef 是不可能的。解决方法?

java - Jpanel布局问题,显示两列按钮和标签,怎么办?

xaml - 在 XAML 命名空间中找不到类型

c++ - 未检测到重载的运算符

c++ - 初始化使用模板参数作为类型的模板化类的静态成员?

ruby-on-rails - 为什么 `layout nil` 不起作用?

android - 将布局划分为 android 中的图 block 的最佳实践?

c# - 展开扩展器后如何滚动以便控件可见