Use of WPF ResourceDictionary

Source: Internet
Author: User

Role: In an application, a window needs to use style, but the style is very much, write in a window code classification inconvenient. The best style is written in a dedicated XAML file and then referenced to the window, just as HTML references an external CSS file.

The original idea is that you can implement shared resources between multiple projects, a resource dictionary is just a simple XAML document that doesn't do anything other than storing the resources you want to use.

1. Create a resource dictionary

The process of creating a resource dictionary is simple, except that all the resources you need to use are contained within a single XAML file. As the following example (FileName Test.xaml, corresponds to the content in the following App.xaml file):

<?xml version= "1.0" encoding= "Utf-8"?>

<!--This file was auto generated by xdraw.-->

<!--do not modify the this file directly, or your changes would be overwritten.-->

<resourcedictionary xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http// Schemas.microsoft.com/winfx/2006/xaml ">

<lineargradientbrush x:key= "Fadebrush" >

<gradientstop color= "Red" offset= "0"/>

<gradientstop color= "Gray" offset= "1"/>

</LinearGradientBrush>

</ResourceDictionary>

Note: When creating a resource, make sure that the compilation option for the resource file is page, which guarantees that the XAML resource file can eventually be compiled into a BAML file. But if set to resource is also a good choice, so that it can be embedded in the assembly, but not compiled, of course, the speed of its resolution is slightly slower.

2. using a resource dictionary

2.1 Integration Resources

If you use a resource dictionary, you first integrate the resource dictionary into some of the resource collections of your application. The general practice is to integrate in the App.xaml file. The code is as follows:

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<resourcedictionary source= "Test.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

2.2 Working with Resources

Once integrated, these resources can be used in the current project. Here's how to use it:

<window x:class= "Hellowpf.mainwindow"

Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"

Title= "MainWindow" height= "340" width= "406" windowstartuplocation= "Centerscreen"

icon= "Sc.ico" >

<grid height= "304" width= "374" >

<Grid.RowDefinitions>

<rowdefinition height= "Auto"/>

<rowdefinition height= "Auto"/>

<rowdefinition height= "*"/>

</Grid.RowDefinitions>

<button margin= "121,30,107,230" grid.row= "2" click= "Button_Click" background= "{StaticResource Fadebrush}" >

</Button>

</Grid>

</Window>

The way to use resources is simple, just use the StaticResource keyword to add.

2.1 Internal Integration

or internal integration referencing external resources in Windows, Note: Adding an external style to a window requires you to specify key, which is not required in application, so here Fadebrush is the key that references the external style Test.xaml

<window x:class="Mssqldoccreator.mainwindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="Clr-namespace:mssqldoccreator"

title="MainWindow" height="602" width="425" windowstartuplocation="Centerscreen" >

<window.resources>

<ResourceDictionary x:key="Fadebrush">

<resourcedictionary.mergeddictionaries>

<ResourceDictionary source="Test.xaml" />

</resourcedictionary.mergeddictionaries>

</ResourceDictionary>

</window.resources>

....

</Window>

Apply a style to the layout of a window

<grid resources="{StaticResource Rdstyle}" >

</Grid>

3. Summary:

There are two main reasons to use a resource dictionary:

A. Provide skin function.

B. Store the content that needs to be local (error message string, etc. to implement soft coding)

The use of the process is relatively simple, summed up mainly the following steps:

1. Create a resource dictionary file

2. Resource dictionary Integration

3. Use the resources in the dictionary

Use of WPF ResourceDictionary

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.