When creating a resource, we usually right-click the root directory of this solution to create a new resource file, which is just a collection of any typed objects, not elements.
For example:
1 < Lineargradientbrush Endpoint = "1, 0" X: Key = "Brush1" > 2 < Gradientstop Color = "Yellow" Offset = "0" /> 3 < Gradientstop Color = "Orange" Offset = ". 7" /> 4 < Gradientstop Color = "Darkred" Offset = "1" /> 5 </ Lineargradientbrush >
View code
When other XAML files (mainwindow. XAML or a view) need to use it
<EllipseFill="{Staticresource brush1}" />
However, I remember the resource at the <application. Resources> level or merge level.
Add the following open app. XAML. Inside the <application. Resources> tag
1 < Resourcedictionary > 2 < Resourcedictionary. mergeddictionaries > 3 < Resourcedictionary Source = "Brushes. XAML" /> 4 </ Resourcedictionary. mergeddictionaries > 5 </ Resourcedictionary >
View code
Note:
- Merge's source should be clearly written path:
1 < Resourcedictionary > 2 < Resourcedictionary. mergeddictionaries > 3 < Resourcedictionary Source = "Brushes. XAML" /> 4 </ Resourcedictionary. mergeddictionaries > 5 </ Resourcedictionary >
View code
If the resource is in the resources folder, merging that into app. XAML wocould look like as follows:
<ResourcedictionarySource= "Resources/brushes. XAML" />
-
- For other referenced assemblies. to base on Pack URI syntax. For example:
<ResourcedictionarySource= "/Myclasslibrary; component/resources/brushes. XAML" />
- For duplicat resources, sort the rules. There is the first key to write the internal key, then the brush defined in the last resource, and then the second
< Resourcedictionary > < Solidcolorbrush Color = "Blue" X: Key = "Brush1" /> < Resourcedictionary. mergeddictionaries > < Resourcedictionary Source = "Resources/brushes2.xaml" /> < Resourcedictionary Source = "Resources/brushes. XAML" /> </ Resourcedictionary. mergeddictionaries > </ Resourcedictionary >