Delpin Susai Raj Thursday 16 December 2021

Xamarin.Forms - Style Inheritance

In this blog post, you will learn how to implement Style Inheritance in Xamarin.Forms app.

Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Style Inheritance

Style inheritance is performed by setting the Style.BasedOn property to an existing Style. In XAML, this is achieved by setting the BasedOn property to a StaticResource markup extension that references a previously created Style.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself. 

Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform. 

BaseStyle

Now, I'm going to create the base style for buttons in App.Xaml

App.Xaml

Style Inheritance

RedButtonStyle

Here, I'm going to inhert base style into the RedbuttonStyle in App.Xaml. See below example

RedButtonStyle.xaml

GreenButtonStyle

Here, I'm going to inhert base style into the RedbuttonStyle in App.Xaml. See below example

GreenButtonStyle.xaml

Consume Style

Now, I'm going to consume the style into my button.

MainPage.Xaml

Run 

I hope you have understood you will learn how to implement Style Inheritance in Xamarin.Forms.

Thanks for reading. Please share your comments and feedback. 

Happy Coding :)