首页 > 解决方案 > 如何在 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,这样即使视图的其余部分是特定于平台的,页脚仍然出现在视图的底部?

标签: c#xamarinmobilexamarin.androidxamarin.ios

解决方案


我们无法将特定平台的文件直接引用到 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


推荐阅读