【问题标题】:Different StaticResource Style in iOS and AndroidiOS 和 Android 中不同的 StaticResource 风格
【发布时间】:2021-09-02 20:36:30
【问题描述】:

我有一个来自 StaticRecourse 的样式的条目,但是 iOS 和 Android 是否可以有 2 种不同的样式

<Entry
                        x:Name="Partijladen"
                        Grid.Row="15"
                        Grid.Column="4"
                        Grid.ColumnSpan="2"
                        Keyboard="Numeric"
                        Style="{StaticResource AEntry}"
                        TextChanged="Partijladen_TextChanged" />

这是可行的,但我想要一个不同的 Android 风格让我们说“BEntry”。我在这里改变什么是不可能的

 Style="{StaticResource AEntry}"

在 App.xaml 中

<Style x:Key="AEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="#2c2c2e" />
        <Setter Property="WidthRequest" Value="80" />
        <Setter Property="HeightRequest" Value="10"  />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="HorizontalTextAlignment" Value="Center" />
    </Style>
    <Style x:Key="BEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="DarkGreen" />
        <Setter Property="FontSize" Value="25" />
        <Setter Property="WidthRequest" Value="80" />
        <Setter Property="HeightRequest" Value="10"  />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="HorizontalTextAlignment" Value="Center" />
    </Style>

试过了,但是出错了

【问题讨论】:

    标签: xaml xamarin.forms staticresource


    【解决方案1】:

    当你有两种不同的风格时:

    <Entry
        ...
        Style="{x:OnPlatform Android={StaticResource AEntry},
                             iOS={StaticResource BEntry}}" />
    
    

    或者,您可以在同一样式中设置特定于平台的样式属性。

    <Style x:Key="AEntry" TargetType="Entry">
        <Setter Property="FontSize" Value="{x:OnPlatform UWP='', iOS='22', Android='20'}" />
        ...
    </Style>
    

    【讨论】:

    • 感谢 Shwa 的回答,当我尝试您的示例时,它给出了一个错误。在 iOS 预期 '}' 之后尝试了很多选项,但不知道放在哪里
    • 用 App.xaml 中的样式更新问题
    • @Bassies 对不起,在“ios”之前漏掉了一个逗号(这里输入的不是复制的),更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多