Search code examples
c#data-bindingxamarin.formsmvvm-lightdynamicresource

How to dynamically change resource of BackgroundColor?


I have following control. I want to change background color on some event trigger. I want to make this clrGray resource as color on some event click.

I have tried by following way but it didn't work :(

XAM:

<local:RoundedFrame x:Name="MyFrame1" HeightRequest="16" IsVisible="True" BackgroundColor="{DynamicResource clrGreen}">

CS:

//On Some event
    //Not working
    MyFrame1.SetDynamicResource(MyFrame1.BackgroundColor, "clrGreen");

Solution

  • Use Xamarin.Forms dynamic resources. Below is a link to the Xamarin documentation:

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/dynamic/

    They are very easy to use/implement, and do exactly what you want.