如何显示派生 XAML 中的 GUI 元素



我成功地继承了类 ContentPage,派生的类称为 SubContentPage。
它包含一些模板元素(按钮和标题标签(。我有两个类FilterPage和SettingsPage,它们继承自SubContentPage。当我显示这些派生页面之一时,我只能看到子内容页面的 GUI 内容,尽管当过滤器页面和设置页面继承自之前的内容时,它起作用了。所以我的 GUI 代码没有错。

我的想法是SubContentPage和派生类不使用显示图形控件的相同内容。因此,派生的 GUI 元素将被忽略。我尝试为类SubContentPage和FilterPage使用相同的StackLayout名称。但这行不通。

SubContentPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="solna_app.GUI.Pages.SubContentPage">
    <ContentPage.Content>
        <StackLayout>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Image x:Name="BackIcon" HorizontalOptions="Start" 
                    Grid.Row="0" />
                <Label x:Name="TitleLabel" HorizontalOptions="Center"
                    FontSize="Large" FontAttributes="Bold"
                    TextColor="Black" Grid.Row="0" />
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

FilterPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<d:SubContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="clr-namespace:solna_app.GUI.Pages;assembly=solna_app"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="solna_app.GUI.Pages.FilterPage">
    <d:SubContentPage.Content>
        <StackLayout>
            <Label  x:Name="SearchLabel" Text="Search keyword" 
                TextColor="Black" FontSize="Large" Margin="10,10,0,0" />
            <Editor x:Name="SearchEditor" Placeholder="Type a keyword"
                PlaceholderColor="Gray" Margin="10,10,10,0"
                HorizontalOptions="CenterAndExpand"/>
        </StackLayout>
    </d:SubContentPage.Content>
</d:SubContentPage>

我希望显示SubContentPage的UI控件和派生类的UI控件。

您可以使用控件模板来实现这一点

1.您可以在应用程序级别定义控件模板,App.xmal

<Application.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="SubContentPageTemplate">
             <StackLayout>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Image x:Name="BackIcon" HorizontalOptions="Start" 
                           Grid.Row="0" />
                    <Label x:Name="TitleLabel" HorizontalOptions="Center"
                           FontSize="Large" FontAttributes="Bold"
                           TextColor="Black" Grid.Row="0" />
                </Grid>
                <ContentPresenter  />
            </StackLayout>
        </ControlTemplate>
    </ResourceDictionary>
</Application.Resources>

2.在FilterPage.xaml中使用

<ContentPage>
    <ContentView ControlTemplate="{StaticResource SubContentPageTemplate}">
     <StackLayout>
        <Label  x:Name="SearchLabel" Text="Search keyword" 
            TextColor="Black" FontSize="Large" Margin="10,10,0,0" />
        <Editor x:Name="SearchEditor" Placeholder="Type a keyword"
            PlaceholderColor="Gray" Margin="10,10,10,0"
            HorizontalOptions="CenterAndExpand"/>
     </StackLayout>
   </ContentView>   
</ContentPage>

您可以参考控件模板的更多信息

最新更新