Thursday, July 7, 2022

Styles in .NET MAUI

 A style is a collection of setters targeted at a specific type of control. .NET MAUI requires a target type so it can make sure that the properties in your setters exist on that type.

You typically define styles as resources inside a ResourceDictionary object. A resource dictionary makes it easy to use the style across multiple controls on the same page, or even across your entire application.

Example:

XML

<Style x:Key="MyButtonStyle" TargetType="Button"> <Setter Property="BackgroundColor" Value="#2A84D3" /> <Setter Property="BorderColor" Value="#1C5F9B" />

<Setter Property="BorderWidth" Value="2" /> <Setter Property="TextColor" Value="Green" /> </Style>


Applying a Style

<Button Text="OK" Style="{StaticResource MyButtonStyle}" /> <Button Text="Cancel" Style="{StaticResource MyButtonStyle}" />


Use an implicit style for multiple controls

Suppose your UI has 20 buttons and you want to apply the same style to all of them. With what we know so far, you'd need to assign to the Style property on each button manually. I

An implicit style is a style that you add to a resource dictionary without giving it an x:Key identifier. Implicit styles are automatically applied to all controls of the specified TargetType object.

XML

<ContentPage.Resources> <Style TargetType="Button"> <Setter Property="BackgroundColor" Value="Blue" /> <Setter Property="BorderColor" Value="Navy" /> ... </Style> </ContentPage.Resources>


Application level resource and style

XML

<Application.Resources> <Color x:Key="MyTextColor">Blue</Color> </Application.Resources>

No comments:

Post a Comment

Open default email app in .NET MAUI

Sample Code:  if (Email.Default.IsComposeSupported) {     string subject = "Hello!";     string body = "Excellent!";    ...