【问题标题】:Xamarin: Style OnPlatform and DynamicResourceXamarin:样式 OnPlatform 和 DynamicResource
【发布时间】:2019-07-14 16:43:34
【问题描述】:
我已经定义了一种风格,但我想将 Android 和 iOS 值设置为 DynamicResource - 语法是什么?
<Style
TargetType="SearchBar" ApplyToDerivedTypes="true">
<Setter Property="BackgroundColor" Value="{OnPlatform Android='White', iOS='#4F8BBF'}"/>
</Style>
【问题讨论】:
标签:
xamarin
xamarin.forms
【解决方案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"];