c# - 我可以在 Xaml 中使用 Device.OnPlatform 设置设备特定 View 吗?

标签 c# xaml xamarin xamarin.forms

我正在尝试在 Xaml 中的不同平台上呈现设备特定 View 。

查看this link我可以看到我可以在 Xaml 中使用 Device.OnPlatform 设置属性,如下所示:

<ContentPage.Resources>
<ResourceDictionary>
<OnPlatform x:Key=”standardFont” x:TypeArguments=”Font”>
<OnPlatform.iOS>12</OnPlatform.iOS>
<OnPlatform.Android>12</OnPlatform.Android>
<OnPlatform.WinPhone>18</OnPlatform.WinPhone>
</OnPlatform>
</ResourceDictionary>
</ContentPage.Resources>

但我想像这样在每个平台上设置 View :

<ContentPage.Resources>
<ResourceDictionary>
<OnPlatform x:Key=”NumericTextBox” x:TypeArguments=”View”>
<OnPlatform.iOS><sf:NumericTextBox/></OnPlatform.iOS>
<OnPlatform.Android><sf:NumericTextBox/></OnPlatform.Android>
<OnPlatform.WinPhone><Entry/></OnPlatform.WinPhone>
</OnPlatform>
</ResourceDictionary>
</ContentPage.Resources>

我需要这个的原因是因为 Syncfusion NumericTextBox 在 Windows 上不起作用(我已经报告了这个错误,但这是一个解决方法)

这可能吗?

PS. 我知道自定义渲染器,但在这种情况下,因为 Syncfusion 数字文本框已经有一个自定义渲染器,所以我不能使用这种技术

最佳答案

你几乎拥有它。你只需要使用一个 ContentView 将其内容属性设置为您的静态资源。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XFApp3.Page1">
   <ContentPage.Resources>
     <ResourceDictionary>
       <OnPlatform x:Key=”NumericTextBox” x:TypeArguments=”View”>
       <OnPlatform.iOS><sf:NumericTextBox/></OnPlatform.iOS>
       <OnPlatform.Android><sf:NumericTextBox/></OnPlatform.Android>
       <OnPlatform.WinPhone><Entry/></OnPlatform.WinPhone>
       </OnPlatform>
     </ResourceDictionary>
   </ContentPage.Resources>
   <!-- That's the line you are missing -->
   <ContentView Content="{StaticResource NumericTextBox}" ></ContentView>
</ContentPage>

您已准备好输入一些数字。

关于c# - 我可以在 Xaml 中使用 Device.OnPlatform 设置设备特定 View 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611219/

相关文章:

c# - 是否可以在 C# 中重载 nameof 运算符?

c# - 如何将WPF效果颜色绑定(bind)到ControlTemplate的前景或背景

wpf - WPF中的自定义UserControl/ ListView 控件

c# - 应用程序在模拟器中运行良好,但在设备上抛出 NSUnknownKeyException 异常

c# - 错误 CS1540/CS0122 : Getting keyboard size doesn't work after switching to Unified API

c# - 具有 FileHelpers 的多个 CSV 结构

c# - 使用 C# 在 Word docx 中填充文档变量

c# - 在foreach循环中插入列表时如何处理事务

c# - 在 XAML 中设置 DataContext

Xamarin:无法将存档上传到 AppStore