首页 > 解决方案 > 使用 OnPlatform 时,无法让标签在 ResourceDictionary 中工作的 FontSize

问题描述

使用时,<Style TargetType="Label">我无法让 FontSize 与 NamedSize 一起使用。

使用 Xamarin.Forms 版本:3.4.0.1008975

在职的 :

<Style TargetType="Label">
    <Setter Property="FontSize" Value="Small" />
</Style>

不工作:

<Style TargetType="Label">
            <Setter Property="FontSize">
                <Setter.Value>
                    <OnPlatform x:TypeArguments="x:String"
                                Android="Small"
                                iOS="Medium"/>
                </Setter.Value>
            </Setter>
        </Style>

我试图将 TypeArguments 更改为<OnPlatform x:TypeArguments="NamedSize" .../>没有成功。

我正在使用其他属性设置器<OnPlatform .../>,它们可以正常工作。它只是字体大小。

标签: xamlxamarin.forms

解决方案


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App67"
             x:Class="App67.MainPage">

    <ContentPage.Resources>
        <OnPlatform x:Key="textColor" 
               x:TypeArguments="Color">
            <On Platform="WinPhone" Value="Red" />
            <On Platform="Android" Value="Aqua" />
            <On Platform="iOS" Value="#80FF80" />
        </OnPlatform>
        <OnPlatform x:Key="fontSize" 
               x:TypeArguments="Font">
            <On Platform="WinPhone" Value="Bold,Large" />
            <On Platform="Android" Value="Bold,Large" />
            <On Platform="iOS" Value="Bold,Large" />
        </OnPlatform>

    </ContentPage.Resources>

    <ContentPage.Content>
        <StackLayout>
            <Label Text="Check out my style." TextColor="{StaticResource textColor}" Font="{StaticResource fontSize}" />
        </StackLayout>
    </ContentPage.Content>

</ContentPage>

推荐阅读