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