MergedDictionaries(学习)

创建Resource的时候我们一般在本Solution根目录下右键创建新的resource文件,is just a collection of any typed objects, not elements.

比如:

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

其他xaml文件(MainWindow.xaml或者某个view)要使用它的时候

<Ellipse Fill="{StaticResource brush1}" />

但是之前记得在<Application.Resources>级,或者本级merge这个资源

如下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

注意:

  1. Merge的source要写清楚Path:
    1 <ResourceDictionary>
    2 <ResourceDictionary.MergedDictionaries>
    3 <ResourceDictionary Source="Brushes.xaml" />
    4 </ResourceDictionary.MergedDictionaries>
    5 </ResourceDictionary>
    View Code

    if 资源在Resources文件夹下, merging that into App.xaml would look like as follows:

    <ResourceDictionary Source="Resources/Brushes.xaml" />
  2. 对于在其他referenced assemblies. 要Base on pack URI syntax.比如:
    <ResourceDictionary Source="/MyClassLibrary;component/Resources/
    Brushes.xaml" />
  3. 对于Duplicat的资源,排序原则。有写内部Key的先,然后是最后一个resource里定义过的笔刷,然后是倒出第二个
    <ResourceDictionary>
    <SolidColorBrush Color="Blue" x:Key="brush1" />
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Resources/Brushes2.xaml" />
    <ResourceDictionary Source="Resources/Brushes.xaml" />
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
原文地址:https://www.cnblogs.com/shawnzxx/p/3077869.html