wpf--notes in layman's (2015.03.29)

Source: Internet
Author: User

There are two ways to use resources: static and dynamic. Static resource use (StaticResource) refers to the one-time use of resources when the program loads into memory, and then no longer accesses the resource, and dynamic Resource usage (DynamicResource) refers to resources that are still accessed while the program is running. Examples are as follows:

XAML Code:

<window x:class= "Mytest.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:local= "Clr-namespace:mytest"
Title= "WPF" height= "205" width= ">"
<Window.Resources>
<textblock x:key= "res1" text= "Sea Life Moon"/>
<textblock x:key= "Res2" text= "Sea Life Moon"/>
</Window.Resources>
<StackPanel>
<button margin= "5, 5, 5, 0" content= "{StaticResource resourcekey=res1}"/>
<button margin= "5, 5, 5, 0" content= "{DynamicResource resourcekey=res2}"/>
<button margin= "5, 5, 5, 0" content= "Update" click= "Button_Click"/>
</StackPanel>
</Window>

C # code:

public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();

}

private void Button_Click (object sender, RoutedEventArgs e)
{
This. resources["res1"] = new TextBlock {Text = "Tianya total at this time"};
This. resources["Res2"] = new TextBlock {Text = "Tianya total at this time"};
}
}

======================================================================

Resource files in the target file in the form of binary data, forming the target file of the resource segment (Resource section), the data will be extracted when used, embedded resources for the application; The resource dictionary is called "WPF resource" or "Object resource", which says the application's embedded resource is " assembly resource or binary resource. In particular, the resources written in the <Application.Resources>...</Application.Resources> tag in WPF programs are still WPF resources, not binary resources.

To add a resource that is a string rather than a file, you can use the Resources.resx resource file in the properties namespace. Resources.resx the organization of the contents of the file is also a "key-value" pair, after compiling, Resources.resx will form the resources class in the Prperties namespace, use the method or property of this class to get the resource. In order for the XAML compiler to access this class, be sure to change the Resources.resx access level from internal to public. Using resources in Resources.resx in XAML code, you first map the program's properties namespace to a XAML namespace, and then use the x:static label extension to access the resource. Examples are as follows:

XAML Code:

<window x:class= "Mytest.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:local= "Clr-namespace:mytest"
xmlns:prop= "Clr-namespace:mytest.properties"
Title= "WPF" height= "205" width= ">"
<grid margin= "5" >
<Grid.ColumnDefinitions>
<columndefinition width= "Auto"/>
<columndefinition width= "4"/>
<columndefinition width= "*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<rowdefinition height= "/>"
<rowdefinition height= "4"/>
<rowdefinition height= "/>"
</Grid.RowDefinitions>
<textblock text= "{x:static prop:Resources.UserName}"/>
<textblock x:name= "Textblockpassword" grid.row= "2"/>
<textbox borderbrush= "Black" grid.column= "2"/>
<textbox borderbrush= "Black" grid.column= "2" grid.row= "2"/>
</Grid>
</Window>

C # code:

public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();

This.textBlockPassword.Text = Properties.Resources.Password;
}
}

The biggest advantage of using RESOURCES.RESX is that it facilitates the internationalization and localization of programs.

Use external files as resources in WPF, just simply add them to your project. Note: If you want an external file to be compiled into a binary resource, you must set the file's BuildAction property value to resource in the Properties window.

WPF access to binary resources has its own set of methods, called the Pack URI path, in the format:

pack://application,,, [assembly name;] [optional version number;] [folder name/] file name.

Common formats are as follows:

[folder name/] File name

This article is from the "Ink Pool small" blog, please be sure to keep this source http://306702895.blog.51cto.com/8366753/1626215

wpf--notes in layman's (2015.03.29)

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.