You probably have used Static values inside ResourceDictionary for your Xamarin Forms App to define common Color, Sting, Double, etc, and have fetched the values using StaicResource or DynamicResource with Static value key. In this blog, we will see when to use StaticResource & when to use DynamicResource to understand the difference between them.
As the name suggests StaticResource, It is a resource value that is static inside the scope where it’s defined.
In the above code snippet, you can see PrimaryColor Static value used for both Frame & Button Background Color. But what if I want to change the color at runtime? We can use the below code snippet to achieve this.
But this won’t work because we have used StaticResource keyword to fetch the color. To see this change of resource value to reflect in UI. We have to use DynamicResource.
As the name suggests again the value of this resource is dynamic which means it can change at runtime.
The only difference between this & previous code snippet is the keywords StaticResource & DynamicResource. Which then helps us to see the changed color value on UI at Runtime.
The same button click event code will work which we used earlier to change the resource value.
Do follow me for more Xamarin Forms Basic concepts & let me know what all topics to cover in the future.