

Xamarin Monkeys: Xamarin.Forms - Style Inheritance
source link: https://xamarinmonkeys.blogspot.com/2021/12/xamarinforms-style-inheritance.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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
<?xml version="1.0" encoding="utf-8"?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="XamarinApp.App"> <Application.Resources>
<Style x:Key="BaseButtonStyle" TargetType="Button"> <Setter Property="TextColor" Value="White" /> <Setter Property="BorderWidth" Value="1" /> <Setter Property="BorderColor" Value="Blue" /> <Setter Property="FontSize" Value="20" />
</Style>
</Application.Resources> </Application>
Style Inheritance
RedButtonStyle
Here, I'm going to inhert base style into the RedbuttonStyle in App.Xaml. See below example
RedButtonStyle.xaml
<?xml version="1.0" encoding="utf-8"?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="XamarinApp.App"> <Application.Resources>
<Style x:Key="BaseButtonStyle" TargetType="Button"> <Setter Property="TextColor" Value="White" /> <Setter Property="BorderWidth" Value="1" /> <Setter Property="BorderColor" Value="Blue" /> <Setter Property="FontSize" Value="20" />
</Style>
<Style x:Key="RedButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> <Setter Property="BackgroundColor" Value="Red" /> </Style>
</Application.Resources> </Application>
GreenButtonStyle
Here, I'm going to inhert base style into the RedbuttonStyle in App.Xaml. See below example
GreenButtonStyle.xaml
<?xml version="1.0" encoding="utf-8"?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="XamarinApp.App"> <Application.Resources>
<Style x:Key="BaseButtonStyle" TargetType="Button"> <Setter Property="TextColor" Value="White" /> <Setter Property="BorderWidth" Value="1" /> <Setter Property="BorderColor" Value="Blue" /> <Setter Property="FontSize" Value="20" />
</Style>
<Style x:Key="GreenButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> <Setter Property="BackgroundColor" Value="Green" /> </Style>
<Style x:Key="RedButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> <Setter Property="BackgroundColor" Value="Red" /> </Style>
</Application.Resources> </Application>
Consume Style
Now, I'm going to consume the style into my button.
MainPage.Xaml
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:xct="http://xamarin.com/schemas/2020/toolkit" x:Class="XamarinApp.LoginPage"> <ContentPage.Resources> </ContentPage.Resources> <ContentPage.Content> <StackLayout HorizontalOptions="Fill" Margin="50,100" VerticalOptions="Start"> <Label Text="Style Inheritance" FontSize="Large"/> <Button Style="{StaticResource RedButtonStyle}" Text="Red Button"/> <Button Style="{StaticResource GreenButtonStyle}" Text="Green Button"/>
</StackLayout> </ContentPage.Content> </ContentPage>
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 :)
Recommend
-
101
-
64
Change Randomly A View’s Color Hi everyone, here is the third part of the Xamarin Forms Tips series. Today, we will look at how to change a...
-
54
Hot off the press, another PR just got merged into the Xamarin.Forms repository. This time, adding a new feature on a Label, the ability to specify
-
56
For a pull request I opened on the Xamarin .Forms repository, I had imple...
-
12
Xamarin forms – Switch custom font on Style Trigger Just a small reminder for myself. I was actually unaware this was possible in XAML styling, but now that I know it can be done, I will be using this more often
-
24
Style Flyout Items with Visual States in Xamarin.Forms250 views•Dec 26, 2020150ShareSave
-
6
Xamarin.Forms - Fingerprint Authentication in your App In this blog post, you will learn how to implement fingerprint, password and face-lock Authentication in your XamarinApp.
-
6
Xamarin.Forms - Working with Behaviors in Xamarin.froms In this blog post, you will learn how to use Behaviors in Xamarin.Forms App Introduction Xamarin.Forms code runs on multiple platforms - each...
-
25
Xamarin.Forms - ContentView Lifecycle In this blog post, you will learn how to use OnAppearing and OnDisAppearing in ContentView in Xamarin forms. Introduction Xamarin.Forms code runs on multiple pla...
-
4
Xamarin.Forms - Validation using Xamarin Community Toolkit In this blog post, you will learn how to validate xaml elements using Xamarin Community Toolkit in Xamarin.Forms App Introduction Xamarin.Fo...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK