Learn how to make your .NET MAUI applications visually appealing and consistent with various styling techniques.
Styles are a powerful mechanism for defining the appearance of your UI elements. They allow you to create reusable definitions for properties like color, font, size, and more, ensuring a cohesive look across your application.
Styles can be defined in several places:
Here's an example of a style definition in App.xaml
:
<Application xmlns="..."
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyMauiApp.App">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="{StaticResource PrimaryColor}" />
<Setter Property="TextColor" Value="White" />
<Setter Property="CornerRadius" Value="5" />
<Setter Property="Padding" Value="10, 5" />
</Style>
<Color x:Key="PrimaryColor">#0078d4</Color>
</ResourceDictionary>
</Application.Resources>
</Application>
Once defined, you can apply styles to individual elements or inherit them.
You can assign a style to an element's Style
property:
<Button Text="Click Me" Style="{StaticResource MyButtonStyle}" />
If you define a style for a specific control type (e.g., Button
) without a x:Key
, it will be applied implicitly to all elements of that type within its scope.
.NET MAUI supports a form of CSS-like cascading for styling. You can use CSS files to define styles and link them to your application.
Create a .css
file (e.g., styles.css
) in your project and add it to your App.xaml
's Resources
:
<Application ...>
<Application.Resources>
<ResourceDictionary Source="styles.css" />
</ResourceDictionary>
</Application>
Example styles.css
:
/* styles.css */
Button {
background-color: #00bcf2;
color: white;
font-size: 16;
border-radius: 8;
padding: 12, 8;
}
.primary-button {
background-color: #0078d4;
color: white;
}
.secondary-button {
background-color: #107c10;
color: white;
}
In XAML, you can apply styles defined in CSS using selectors:
<Button Text="Primary Action" StyleClass="primary-button" />
<Button Text="Secondary Action" StyleClass="secondary-button" />
<Label Text="Hello, MAUI!" FontSize="20" TextColor="DarkGray" />
For quick, one-off styling, you can set properties directly on an element:
<Label Text="Styled Directly"
FontSize="22"
TextColor="Purple"
FontAttributes="Bold" />
For more advanced theming, consider using control templates or dynamic resource references that can change based on an application-wide theme setting.
Here are some commonly used properties you'll style:
BackgroundColor
TextColor
FontSize
FontFamily
FontAttributes
(Bold, Italic)Padding
Margin
BorderColor
BorderRadius
HeightRequest
WidthRequest
You can define how controls look when they are in different states (e.g., pressed, disabled). This is often done using VisualStateManager
within control templates.
Let's see how to apply different styles to buttons using CSS classes.
Applying styles via StyleClass
from styles.css
:
<!-- In your XAML page -->
<StackLayout Spacing="15">
<Button Text="Default MAUI Button" />
<Button Text="Primary Action" StyleClass="primary-button" />
<Button Text="Secondary Action" StyleClass="secondary-button" />
</StackLayout>
Rendered appearance:
Defining and using more specific CSS rules:
<!-- In your XAML page -->
<StackLayout Spacing="15">
<Button Text="Styled Button" StyleClass="custom-button" />
<Button Text="Another Button" />
</StackLayout>
/* In your styles.css */
.custom-button {
background-color: #9370DB; /* MediumPurple */
color: white;
font-size: 18;
font-weight: bold;
border-radius: 20; /* More rounded */
padding: 15, 10;
border: 2px solid #6A5ACD; /* SlateBlue */
}
Rendered appearance: