简体   繁体   中英

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:

    <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.

The regular way of using these according to the internet is to use <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.

Suppose that you have ResourceDictionary in MyDictionary.xaml file.

<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.

Let's create MyDictionary.xaml.cs with class MyDictionary (name can be different from the name of the xaml file).

public partial class MyDictionary
{
    public MyDictionary()
    {
        InitializeComponent();
    }
}

A class must be a partial class. The constructor must be added to the class and InitializeComponent method must be called. The InitializeComponent method will be automatically generated for the class if you set the x:Class attribute in MyDictionary.xaml

Now you can reference MyDictionary in MergedDictionaries

<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)

<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.

Example of usage of data template defined in 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"/> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM