Quantcast
Channel: User Liyun Zhang - MSFT - Stack Overflow
Viewing all articles
Browse latest Browse all 444

Answer by Liyun Zhang - MSFT for How to override a parent resource dictionary property in a MAUI application?

$
0
0

According to the official document about Merge resource dictionaries from other assemblies:

When merged ResourceDictionary resources share identical x:Keyattribute values, .NET MAUI uses the following resource precedence:

  1. The resources local to the resource dictionary.
  2. The resourcescontained in the resource dictionaries that were merged via theMergedDictionaries collection, in the reverse order they are listed inthe MergedDictionaries property.

And then I created a sample to test:

Here is the structure of my project:

enter image description here

And the code in the App.xaml:

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             xmlns:lib="clr-namespace:MyClassLibraty.Resources;assembly=MyClassLibraty"             xmlns:local="clr-namespace:MauiApp7"             x:Class="MauiApp7.App"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Resources/Styles/Colors.xaml" /><ResourceDictionary Source="Resources/Styles/Styles.xaml" /><lib:MyColor/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources></Application>

I declared <Color x:Key="Primary">#13F444</Color> in the MyColor of the class library and then use it in the page:

<Label     Text="Hello, World!"     TextColor="{StaticResource Primary}"     Style="{StaticResource Headline}"     SemanticProperties.HeadingLevel="Level1" />

Finally, the label used the Primary key in MyColor.xaml of the class library instead of the default Primary key in Colors.xaml:

enter image description here


Viewing all articles
Browse latest Browse all 444

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>