c# - 如何在 Xamarin 中使用具有 android/IOS 特定视图的控制模板
问题描述
我想在我的 Xamarin 应用程序中使用一致的页脚。为了尝试实现这一点,我ControlTemplate
在 App.xaml 类中有一个元素,定义如下:
<ControlTemplate x:Key="MainPageTemplate">
<StackLayout>
<ContentPresenter VerticalOptions="FillAndExpand" />
.
.
.
</StackLayout>
</ControlTemplate>
我在我的 xaml 窗口中使用ControlTemplate="{StaticResource MainPageTemplate}"
ContentPage 标记引用它。
但是,我有两个不是常规内容页面的页面,因为它们都是以特定于平台的方式实现的,一个带有 .axml 文件(.Android 项目),一个带有情节提要文件(.iOS 项目)。
有没有办法让这些文件使用 Xamarin ControlTemplate,这样即使视图的其余部分是特定于平台的,页脚仍然出现在视图的底部?
解决方案
我们无法将特定平台的文件直接引用到 xaml。在你的情况下,如果你想在不同的平台上实现不同的效果,你可以使用Device类,如下所示
<StackLayout>
<StackLayout.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
<On Platform="Android, UWP" Value="0,0,0,0" />
</OnPlatform>
</StackLayout.Margin>
...
</StackLayout>
有关更多详细信息,您可以参考https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/device。
推荐阅读
- c++ - 内存访问冲突
- html - 如何在每个列表项的末尾添加雪佛龙,从右对齐
- deep-learning - 如何在预训练的 BERT 模型之上添加多类多标签层?
- android - 我正在尝试访问外部存储目录,以获取歌曲列表并在 RecyclerView 中显示它们
- javascript - HTML5 Canvas Recording 在新的 Blob 记录时崩溃Chunks
- pug - 如何根据变量渲染块?哈巴狗(翡翠)
- java - JNA:如何在结构中指定可变长度(0+)数组?
- javascript - 在 JavaScript 自适应卡片中提交时禁用提交按钮/加载卡片
- android - FAB 按钮未在 android studio 布局编辑器中正确显示可绘制对象
- apache-nifi - 仅当我们从前一个处理器获取 Request.json 时,如何创建 NiFi 工作流程 - 将所有文件转发到下一个处理器?