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:
- The resources local to the resource dictionary.
- 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:
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: