StaticResource & DynamicResource in Xamarin Forms

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.

Final Output

StaticResource

As the name suggests StaticResource, It is a resource value that is static inside the scope where it’s defined.

StaticResource

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.

Color Change
StaticResource Output

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.

DynamicResource

As the name suggests again the value of this resource is dynamic which means it can change at runtime.

Dynamic Resource

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.

Color Change

The same button click event code will work which we used earlier to change the resource value.

Dynamic Resource Output

Do follow me for more Xamarin Forms Basic concepts & let me know what all topics to cover in the future.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dinesh Falwadiya

Mobile app developer who loves to share his knowledge and get improve with others.