有没有办法在 app.xaml 文件的样式中指定 OnPlatform ?

Posted

技术标签:

【中文标题】有没有办法在 app.xaml 文件的样式中指定 OnPlatform ?【英文标题】:Is there a way to specify an OnPlatform inside of a style in the app.xaml file? 【发布时间】:2019-02-01 03:51:21 【问题描述】:

到目前为止,我的代码如下所示:

<OnPlatform x:Key="smallLabelFontSize" x:TypeArguments="x:Double" ios="12" android="10" WinPhone="14" />

<Style x:Key="smallLabel" TargetType="Label">
   <Setter Property="FontSize" Value="StaticResource smallLabelFontSize" />
   <Setter Property="TextColor" Value="#999999" />
</Style>

有没有一种方法可以使用 OnPlatform 在样式中设置 FontSize 而无需引用 smallLabelFontSize?

我看过这里,但看不到任何示例

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/application

【问题讨论】:

【参考方案1】:

类似:

<Style TargetType="Label">
    <Setter Property="FontSize">
        <Setter.Value>
            <OnPlatform x:TypeArguments="x:Double">
                <On Platform="iOS" Value="12"/>
                <On Platform="Android" Value="10"/>
            </OnPlatform>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

有没有办法指定OnDevice? @jamesfdearborn 问一个关于你想要做什么的新问题。

以上是关于有没有办法在 app.xaml 文件的样式中指定 OnPlatform ?的主要内容,如果未能解决你的问题,请参考以下文章