Xamarin:样式OnPlatform和DynamicResource

时间:2019-07-14 16:43:34

标签: xamarin xamarin.forms

我已经定义了样式,但是我想将Android和iOS值设置为DynamicResource-语法是什么?

<Style
    TargetType="SearchBar" ApplyToDerivedTypes="true">
    <Setter Property="BackgroundColor" Value="{OnPlatform Android='White', iOS='#4F8BBF'}"/>
</Style>

1 个答案:

答案 0 :(得分:1)

每个官方文档针对不同目标的不同值

<!-- Colors -->
        <Color x:Key="AppBackgroundColor">WhiteSmoke</Color>
        <Color x:Key="iOSNavigationBarColor">WhiteSmoke</Color>
        <Color x:Key="AndroidNavigationBarColor">#2196F3</Color>
        <Color x:Key="iOSNavigationBarTextColor">Black</Color>
        <Color x:Key="AndroidNavigationBarTextColor">White</Color>

        <!-- Implicit styles -->
        <Style TargetType="{x:Type NavigationPage}">
            <Setter Property="BarBackgroundColor"
                    Value="{OnPlatform iOS={StaticResource iOSNavigationBarColor},
                                       Android={StaticResource AndroidNavigationBarColor}}" />
             <Setter Property="BarTextColor"
                    Value="{OnPlatform iOS={StaticResource iOSNavigationBarTextColor},
                                       Android={StaticResource AndroidNavigationBarTextColor}}" />           
        </Style>

        <Style TargetType="{x:Type ContentPage}"
               ApplyToDerivedTypes="True">
            <Setter Property="BackgroundColor"
                    Value="{StaticResource AppBackgroundColor}" />
        </Style>

用于设置不同的值

但是,如果您确实要寻找文字动态资源,则必须在构造函数后面的代码中进行设置。

Here is the example

   Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];