Search code examples
c#.netmauimarkup

How to access the Styles.xaml file and use it in the c# markup code


I am new to dotnet maui and I can't figuring out how to use the styles defined in the Styles.xaml sheet in the c# markup code.

When I use the following code, my application crashes

new Label
{
    Text = "Hello, World!",
    Style = (Style)Application.Current.Resources["TitleLabel"]
}

The style exists in the stylesheet:

<Style TargetType="Label" x:Key="TitleLabel"\>
    <Setter Property="FontFamily" Value="Montserrat" /\>
    <Setter Property="FontSize" Value="20" /\>
    <Setter Property="FontAttributes" Value="None" /\>
    <Setter Property="TextColor" Value="{StaticResource Black}" /\>
</Style\>

So how do I get this style to work with my c# markup code?


Solution

  • I was able to access the Colors.xaml file with the following if-statement:

    if (App.Current.Resources.TryGetValue("ReadTag", out var colorvalue))
                StatusTagColor = (Color)colorvalue;