Avoid messing up the WPF resource dictionary

Source: Internet
Author: User
Avoid messing up the WPF resource dictionary
Zhou yinhui

Today, we see a xxxresource. XAML file in the project type.CodeThere are more than two thousand rows, which leads me to think about how to organize ourWPFResources. InWPFThere can be many ways to organize resources, as described below:

1 Each element maintains its own resources. . Yes Resources Attribute objects have many types, such Application, Window , Page , Style , Frameworktemplate , Frameworkelement , Frameworkcontentelement . In this mode, each type of element maintains the resources it needs, without worrying about whether resources can be shared. For example, two Button :
< Button X : Name = "Btn1"

Content= "Button 1"

Foreground= "{DynamicresourceRedbrush} ">

<Button. Resources>

<Solidcolorbrush X:Key= "Redbrush"Color= "Red"/>

</Button. Resources>

</Button>

<ButtonX:Name= "Btn2"

Content= "Button 2"

Foreground= "{DynamicresourceRedbrush} ">

<Button. Resources>

<Solidcolorbrush X:Key= "Redbrush"Color= "Red"/>

</Button. Resources>

</Button>

This causes the problem that resources cannot be shared, and some identical resources are created repeatedly, which affects the efficiency.

2To improve the resource sharing rate. We can move shared resources to a higher level so that elements at the bottom level can directly reference these resources without re-creating them, for example:

<Stackpanel>

<Stackpanel. Resources>

<Solidcolorbrush X:Key= "Redbrush"Color= "Red"/>

</Stackpanel. Resources>

<ButtonX:Name= "Btn1"

Content= "Button 1"

Foreground= "{DynamicresourceRedbrush} ">

</Button>

<ButtonX:Name= "Btn2"

Content= "Button 2"

Foreground= "{DynamicresourceRedbrush} ">

</Button>

</Stackpanel>

We willRedbrushMoved to twoButtonPublic fatherStackpanelSo that they can reference them at the same time.

But in general, for a wider range of sharing, we willResourecesMoveWindow(OrPage) Or evenApp.


However, as resources increaseXAMLFile, then ourXAMLThe file will become very bloated (For example, if you are not careful about thousands of rows, reading will become very poor, and it is not easy to quickly navigate to the resources you want to find ). Therefore, we have to consider how to reorganize our resources to solve this problem.

One feasible way isResourceDifferent TypesResourcedictionaryFile, and then useMergeddictionariesTo merge various resource dictionaries. Before grouping, all resources are in the same resource dictionary, for example:

After grouping by different types (Brushes,Datatemplates,Styles,ConvertersAnd so on.,Each type corresponds to a resource dictionary, so that the resource dictionary is relatively small), such:

Then combine these dictionaries into a dictionary for use:

<Application. Resources>

<Resourcedictionary>

<Resourcedictionary. mergeddictionaries>

<ResourcedictionarySource= "Resources" brushes. XAML "/>

<ResourcedictionarySource= "Resources" styles. XAML "/>

<ResourcedictionarySource= "Resources" datatemplates. XAML "/>

</Resourcedictionary. mergeddictionaries>

</Resourcedictionary>

</Application. Resources>

This obvious advantage is that it makes our resources more structured, better searched and maintained, and more suitableProgramMember's point of view. However, one obvious drawback is thatBlendOfResourceThe multi-level directory structure of the resources you create on the directory panel is not obvious, and the multi-level structure of the target cannot be displayed as a plane structure, this makes it inconvenient for you not to distinguish resource files of the same name under different folders (however, you can move the mouse over the file, itsTooltipThe complete paths are displayed to differentiate them), for example:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.