How to create a main layout template in XAML

前端 未结 1 1844
北海茫月
北海茫月 2020-12-18 10:07

I\'m new to windows development. I\'m making a simple windows app which has a few pages and each page has a similar layout in XAML. Like this:

Each page is

相关标签:
1条回答
  • 2020-12-18 10:59

    One feasible way to do this is using UserControl with ContentPresenter. For example:

    Add a UserControl named MainTemplate. In the XAML, set the layout with ContentPresenter and bind it to the DependencyProperty defined in code-behind.

    <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>
    

    In the code-behind, set the DependencyProperty so that we can use them to set the content in other pages.

    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); }
        }
    }
    

    After this, we can use the UserControl in other pages to reuse the general layout. For example, using it in "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>
    

    Then the "MainPage" will look like follwoing:

    0 讨论(0)
提交回复
热议问题