简体   繁体   English

使用 ResourceDictionary.MergedDictionaries 的不同方式

[英]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.

相关问题 在App.xaml中使用ResourceDictionary.MergedDictionaries - Using ResourceDictionary.MergedDictionaries in App.xaml ResourceDictionary.MergedDictionaries导致奇怪的错误 - ResourceDictionary.MergedDictionaries causes weird errors 在类型“ ResourceDictionary”中找不到可连接属性“ MergedDictionaries” - The attachable property 'MergedDictionaries' was not found in type 'ResourceDictionary' 如何在WPF应用程序中将ResourceDictionary动态添加到MergedDictionaries - How to dynamically add ResourceDictionary to MergedDictionaries in WPF app 如何在其他页面中使用App ResourceDictionary和MergedDictionaries-Windows Phone 8.1 - How to use App ResourceDictionary and MergedDictionaries in other page - Windows Phone 8.1 SelectMany() 的不同使用方式 - Different ways of using SelectMany() 无法将 ResourceDictionary 从 xamarin.forms 添加到 Application.Current.Resources.MergedDictionaries - Cant add ResourceDictionary to Application.Current.Resources.MergedDictionaries from xamarin.forms 从Silverlight中的不同ResourceDictionary引用ResourceDictionary中的资源 - Referencing a resource in a ResourceDictionary from a different ResourceDictionary in Silverlight 了解访问器的不同使用方式 - Understanding different ways of using accessors 在ResourceDictionary文件中使用视图框 - using viewbox in ResourceDictionary file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM