1

I have defined a style , but I'd like to set the Android and iOS values to a DynamicResource - What's the syntax?

<Style
    TargetType="SearchBar" ApplyToDerivedTypes="true">
    <Setter Property="BackgroundColor" Value="{OnPlatform Android='White', iOS='#4F8BBF'}"/>
</Style>
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • https://stackoverflow.com/questions/54682847/label-fontsize-onplatform-typearguments-double-giving-an-error-in-xamarin-forms/54695007#54695007 – SushiHangover Jul 14 '19 at 18:30

1 Answers1

3

Per official documentation for different values for different targets

<!-- 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>

That's for setting different values

But if you are really looking for a literal dynamic resource, you'll have to set it in code behind constructor.

Here is the example

Resources ["searchBarStyle"] = Resources ["blueSearchBarStyle"];
Dennis van Opstal
  • 1,294
  • 1
  • 22
  • 44
Morse
  • 8,258
  • 7
  • 39
  • 64