WPF object-level resource definition and lookup

Source: Internet
Author: User

Article Overview:

This presentation describes how to define WPF object-level resources and access and use object-level resources through XAML code and C #.

Related Downloads (code, screen Recorder): http://pan.baidu.com/s/1hqvJNY8

Online Play: http://v.youku.com/v_show/id_XODA1NTU2Mzky.html

Warm tip : If the screen recording and code can not be downloaded, you can leave a message in the station, or send an email to [email protected]


I. Complete definition and use of resources

<window x:class= "Demo008.mainwindow"        xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:sys= "clr-namespace:system;assembly= mscorlib "        title=" Resource "fontsize=" + loaded= "window_loaded" >    <!--complete notation--    < window.resources>        <ResourceDictionary>            <sys:string x:key= "str" > Sinking boat side thousand sails, the disease tree before the million wood spring. </sys:String>            <sys:double x:key= "dbl" >3.1415926</sys:Double>        </resourcedictionary >    </Window.Resources>    <StackPanel>        <textblock margin= "5" text= "{StaticResource RESOURCEKEY=STR} "/>    </StackPanel></Window>

ii. resource definition and use of shorthand
<window x:class= "Demo008.mainwindow"        xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:sys= "clr-namespace:system;assembly= mscorlib "        title=" Resource "fontsize=" + loaded= "window_loaded" >    <!-----    < window.resources>        <sys:string x:key= "str" > Sinking boat side thousand sails, the disease tree before the million wood spring. </sys:String>        <sys:double x:key= "dbl" >3.1415926</sys:Double>    </window.resources >    <StackPanel>        <textblock x:name= "TextBlock1" margin= "5" text= "{StaticResource str}"/>    </StackPanel></Window>

third, code lookup resources
The usual practice is as follows:
string text = this. FindResource ("str"). ToString (); Textblock1.text = Text;

If you know which object the resource is in, the resource dictionary can be accessed directly using the following method:
string text = this. resources["str"]. ToString (); Textblock1.text = Text;


WPF object-level resource definition and lookup

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.