There are some drawbacks to the way the style file is referenced (excerpt from the official)
When you reference a ResourceDictionary in XAML, a ResourceDictionary object was created each time you reference it. So if you have a custom controls in your library and merge the shared resourcedictionaries for each control by using XAML , you create ten identical ResourceDictionary objects. You can avoid this by creating a static class that returns the ResourceDictionary and merging the resources in code.
Therefore, we can circumvent this problem by the following way
1. Create a resource folder in the base Resource class library resources, create a dictionary resource style in resources Styles.xaml
<lineargradientbrush x:key= "NormalBrush" startpoint= "0,0" endpoint= "0,1" >
<GradientBrush.GradientStops>
<GradientStopCollection>
<gradientstop color= "#Red" offset= "0.0"/>
<gradientstop color= "#Blue" offset= "1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
2. Creating a class in resources
public static Class Shareddictionarystylemanager
{
Static ResourceDictionary _sharedstyledictionary;
public static ResourceDictionary Sharedstyledictionary
{
Get
{
if (_sharedstyledictionary = = null)
{
System.Uri resourcelocater = new System.Uri ("/myglobal.infrustructure;component/resources/styles.xaml", System.UriKind.Relative);
_sharedstyledictionary = (ResourceDictionary) application.loadcomponent (resourcelocater);
}
return _sharedstyledictionary;
}
}
}
3. Create a user control Upjappmenuview.xaml, access the load style before the constructor
Public Upjappmenuview ()
{
Referencing global singleton style code ******************************
This. RESOURCES.MERGEDDICTIONARIES.ADD (shareddictionarystylemanager.sharedstyledictionary);
InitializeComponent ();
}
4. The user control XAML can reference the
<border width= "Auto" height= "Auto" borderbrush= "Blue" borderthickness= "ten" cornerradius= "ten" >
<rectangle stroke= "Black" strokethickness= "2" fill="{StaticResource NormalBrush}"></rectangle >
</Border>
Component user Control reference style resource (ii)---code reference