Global Styles with Xamarin Forms
When working with the UI for your application you will often come across styles and elements which share the same look and feel. What will you do in such circumstances? Rewrite the same width, height, color, font setters? Is there a way to have them at one place and resuse them throughout your app? Sure!
You can declare and define global styles at the application level which can be accessed and used on all pages throughout your application.
This can be done in the App.xaml file in the ResourceDictionary.
Create a Xamarin.Forms application from by going to File -> New Project -> Xamarin.Forms in the Cross Platform section of the templates. Let’s name it “GlobalStylesXamarinForms”
We have defined styles for button, label and entry. Note that they have keys. These keys will be referred to in other pages by us to set these styles to the controls.
Run the app and notice that the label, entry and the button pairs have the same styles applied respectively.
We can also have these styles overridden by setting a particular attribute value of a control in spite of applying a common style, or have a basedon style using the concept of style inheritance.
Change one of your label’s style from LabelStyle to LabelStyle2 and run the app to see the difference.
Username label has the LabelStyle2(which is inherited from the LabelStyle) applied, while the Password label still has LabelStyle. Note that everything else about the two labels except the font size is same.