signalover
signalover

Reputation: 167

Change color of a Resource

I have created a resource in my project as follows:

<ContentPage.Resources>
    <ResourceDictionary>
        <Color x:Key="BgColor">#FFFFFF</Color>
    </ResourceDictionary>
</ContentPage.Resources>

 <RelativeLayout x:Name="Layout" BackgroundColor="{x:DynamicResource BgColor}">

when I try to change the color of the resource, in theory it should change the background color, but it doesn't happen

private void SwitchTheme_Toggled(object sender, ToggledEventArgs e)
{
     App.Current.Resources["BgColor"] = "#282928";
}

Upvotes: 1

Views: 845

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

when I try to change the color of the resource, in theory it should change the background color, but it doesn't happen.

You can try the following code:

  <ContentPage.Resources>
    <Color x:Key="BgColor">red</Color>
</ContentPage.Resources>

  private void btn1_Clicked(object sender, EventArgs e)
    {
        //Resources["BgColor"] = Color.FromHex("#F15A29"); or using
        //Resources["BgColor"] = Color.Green; or using
        Resources["BgColor"] = "#282928";         
    }

If you create ResourceDictionary in App.xaml, you can use Application.Current.Resources["BgColor"] it works fine.

Upvotes: 1

Related Questions