c# - 如何也显示派生 XAML 的 GUI 元素?
问题描述
我成功地从类 ContentPage 继承,派生类称为 SubContentPage。
它包含一些模板元素(按钮和标题标签)。我有两个继承自 SubContentPage 的类 FilterPage 和 SettingsPage。当我显示其中一个派生页面时,我只能看到 SubContentPage 的 GUI 内容,尽管之前从 ContentPage 继承的 FilterPage 和 SettingsPage 可以正常工作。所以我的GUI代码没有错。
我的想法是 SubContentPage 和派生类不使用图形控件出现的相同内容。因此派生的 GUI 元素将被忽略。我尝试对 SubContentPage 和 FilterPage 类使用相同的 StackLayout 名称。但这行不通。
子内容页面.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>
过滤器页面.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 控件。
解决方案
您可以使用ControlTemplate来实现这一点
1.您可以在应用程序级别定义一个 ControlTemplate,在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>
您可以参考ControlTemplate的更多信息
推荐阅读
- django - 错误 Django 'imagem' 属性没有与之关联的文件
- typescript - 如何实现将任何函数类型的返回类型转换为承诺的实用程序类型?
- python - find() 方法找不到我能找到的
- python - psycopg2 copy_to 在插入期间将浮点值解释为字符串
- java - 线程睡眠冻结了我的代码。Java 客户端-服务器通信
- xamarin.forms - Xamarin 在单元格值之间形成笔划线?
- java - IntellijIDEA 2021.2.2 无法识别从 jar 文件导入到 gradle 模块的类
- ffmpeg - 为什么 FFmpeg 从一组图像创建后视频尺寸会发生变化?
- java - 具有动态方法参数类型的 Jackson TypeReference
- c++ - 使用堆栈的内存错误代码 0xC0000005