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?
More often, the
Styleis going to be consumed in the xaml which is fairly straightforward. For example, placing a label on theMainPagethat uses the TitleLabel style could be expressed like this.However, your code seems to be attempting this in code-behind where the recipe might involve querying the
MergedDictionariescollection and selecting the first one withStyles.xamlin itsOriginalStringproperty. I tested this and it works, but I can't say that I recommend it.