[英]Different ways of using ResourceDictionary.MergedDictionaries
I was going through some code in our product and saw some colleagues using ResourceDictionary.MergedDictionaries in a way I had not seen it used before:我正在浏览我们产品中的一些代码,并看到一些同事以我以前从未见过的方式使用 ResourceDictionary.MergedDictionaries:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<toolTips:ToolTips />
<styles:ControlStyles />
<icons:IconDictionary />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
tooltips:ToolTips
and all the other elements in the MergedDictionaries are ResourceDictionaries. tooltips:ToolTips
和 MergedDictionaries 中的所有其他元素都是 ResourceDictionaries。
The regular way of using these according to the internet is to use <ResourceDictionary Source="uri to your xaml file"/>
.根据互联网使用这些的常规方法是使用<ResourceDictionary Source="uri to your xaml file"/>
。
So is there any practical difference between both?那么两者之间有什么实际区别吗?
If this way works why isn't it used more often as it plays well with code completion?如果这种方式有效,为什么不更频繁地使用它,因为它与代码完成配合得很好?
I've used ResourceDicionary
this way only once on a big project and it was benefical in my situation.我只在一个大型项目中以这种方式使用过ResourceDicionary
一次,这对我的情况很有帮助。
Suppose that you have ResourceDictionary
in MyDictionary.xaml
file.假设您在MyDictionary.xaml
文件中有ResourceDictionary
。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="YourNamespace.MyDictionary">
</ResourceDictionary>
You can add an x:Class
attribute to the ResourceDictionary
element and specify the fully qualified name of the code-behind class.您可以将x:Class
属性添加到ResourceDictionary
元素并指定代码隐藏 class 的完全限定名称。
Let's create MyDictionary.xaml.cs
with class MyDictionary
(name can be different from the name of the xaml file).让我们使用 class MyDictionary
MyDictionary.xaml.cs
名称可以与 xaml 文件的名称不同)。
public partial class MyDictionary
{
public MyDictionary()
{
InitializeComponent();
}
}
A class must be a partial class. class 必须是部分 class。 The constructor must be added to the class and InitializeComponent
method must be called.必须将构造函数添加到 class 并且必须调用InitializeComponent
方法。 The InitializeComponent
method will be automatically generated for the class if you set the x:Class
attribute in MyDictionary.xaml
如果在 MyDictionary.xaml 中设置x:Class
属性, MyDictionary.xaml
自动生成InitializeComponent
方法
Now you can reference MyDictionary
in MergedDictionaries
现在您可以在MergedDictionaries
中引用MyDictionary
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:MyDictionary/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
If you add some DataTemplate
into MyDictionary.xaml
you can create event handlers in code-behind (handlers will be automatically generated by VS)如果您将一些DataTemplate
添加到MyDictionary.xaml
,您可以在代码隐藏中创建事件处理程序(处理程序将由 VS 自动生成)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="YourNamespace.MyDictionary">
<DataTemplate x:Key="MyTemplate">
<Button Click="Button_Click"/>
</DataTemplate>
</ResourceDictionary>
Code-behind:代码隐藏:
public partial class MyDictionary
{
public MyDictionary()
{
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
// custom logic
// edit another element, etc.
}
}
If the class is inherited from the ResourceDictionary
class then other resources can be accessed from the code-behind.如果 class 是从ResourceDictionary
class 继承的,则可以从代码隐藏中访问其他资源。
Example of usage of data template defined in MyDictonary
: MyDictonary
中定义的数据模板的使用示例:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:MyDictionary/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl ContentTemplate="{StaticResource MyTemplate}"/>
</Grid>
From my point of view the biggest advantages are that you can encapsulate logic into separated files (it's easy to maintain and add new features in big projects) and avoid referencing ResourceDictionaries
by <ResourceDictionary Source="uri to your xaml file"/>
.在我看来,最大的优点是您可以将逻辑封装到单独的文件中(在大型项目中易于维护和添加新功能),并避免通过<ResourceDictionary Source="uri to your xaml file"/>
引用ResourceDictionaries
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.