1. Create a resource dictionary file, that is, a XAML file.
The syntax format of the file is as follows:
Test. XAML
< Resourcedictionary
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: chartingtoolkit = "CLR-namespace: system. Windows. Controls. datavisualization. charting; Assembly = system. Windows. Controls. datavisualization. toolkit"
Xmlns: visualizationtoolkit = "CLR-namespace: system. Windows. Controls. datavisualization; Assembly = system. Windows. Controls. datavisualization. toolkit" >
<! -- Define style Resources -->
< Style X: Key = "Textblockstyle1" Targettype = "Textblock" >
< Setter Property = "Foreground" Value = "Orange" />
< Setter Property = "Fontsize" Value = "24" />
< Setter Property = "Verticalalignment" Value = "Bottom" />
</ Style >
<! -- Define data template resources -->
< Datatemplate X: Key = "Citydetails" >
< Grid >
< Grid. columndefinitions >
< Columndefinition Width = "Auto" />
< Columndefinition Width = "90" />
</ Grid. columndefinitions >
< Grid. rowdefinitions >
< Rowdefinition />
< Rowdefinition />
</ Grid. rowdefinitions >
< Textblock Text = "Activity :"
Grid. Column = "0"
Grid. Row = "0"
Style =" {Staticresource detailssmalltitle} " />
< Textblock Text =" {Binding activity} "
Grid. Column = "1"
Grid. Row = "0"
Style =" {Staticresource detailssmalltext} " />
......
</ Grid >
</ Datatemplate >
<! -- Define control template resources -->
< Controltemplate X: Key = "Controltemplatetest"
Targettype = "Chartingtoolkit: Chart" >
< Grid >
< Grid. columndefinitions >
< Columndefinition Width = "*" />
< Columndefinition Width = "Auto" />
</ Grid. columndefinitions >
< Grid. rowdefinitions >
< Rowdefinition Height = "Auto" />
< Rowdefinition Height = "*" />
</ Grid. rowdefinitions >
< Visualizationtoolkit: Title Grid. columnspan = "2"
Content =" {Templatebinding title} "
Style =" {Templatebinding titlestyle} " />
......
</ Grid >
</ Controltemplate >
</ Resourcedictionary >
The X: key attribute of the style is the unique identifier of the resource in the resource dictionary. It is also called as a unique key called on other pages.
2. Call resources in a resource
Add a resource dictionary on the mainpage. XAML page. The syntax is as follows:
< Phone: phoneapplicationpage. Resources >
< Resourcedictionary >
< Resourcedictionary. mergeddictionaries >
< Resourcedictionary Source = "Test. XAML" />
</ Resourcedictionary. mergeddictionaries >
</ Resourcedictionary >
</ Phone: phoneapplicationpage. Resources >
Resourcedictionary. mergeddictionaries obtains a collection of resourcedictionary dictionaries, which constitute various resource dictionaries in the merged dictionary.
If you wantProgramLoad all resources at startup. You can add resource loading on the app. XAML page. The syntax is as follows:
< Application
X: Class = "Datavisualizationonwindowsphone. app"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: Shell = "CLR-namespace: Microsoft. Phone. Shell; Assembly = Microsoft. Phone" >
< Application. Resources >
<! -- Add Resource -->
< Resourcedictionary >
< Resourcedictionary. mergeddictionaries >
< Resourcedictionary Source = "Test. XAML" />
</ Resourcedictionary. mergeddictionaries >
</ Resourcedictionary >
</ Application. Resources >
......
</ Application >
3. Use resources in dictionary Resources
Controls on the mainpage. XAML page call custom resources. The syntax is as follows:
Call the style resource whose X: key value is textblockstyle1 in the dictionary resource.
< Stackpanel X: Name = "Contentpanel" Grid. Row = "1" Margin = "12, 0, 12, 0" >
< Textblock Text = "Some text" Style =" {Staticresource textblockstyle1} " />
</ Stackpanel >
Call the data template resource whose X: key value is citydetails in the dictionary Resource
<ContentcontrolContenttemplate="{Staticresource citydetails}"
Horizontalalignment= "Left"
X: Name= "Detailscontrol"Margin= "0, 0, 0, 5" />
Call the control template resource whose X: key value is controltemplatetest in the dictionary resource.
<Charting: ChartX: Name= "MyChart"
Style="{Staticresource phonechartstyle}"
Template="{Staticresource controltemplatetest}">
......
</Charting: Chart>
You can also call dictionary resources on the CS page. The syntax is as follows:
Controltemplate template;
Template=Application. Current. Resources ["Controltemplatetest"]AsControltemplate;
MyChart. Template=Template;
For more information, see the Silverlight resource dictionary in msdn.
Detailed introduction to the Silverlight resource Dictionary of msdn
Http://msdn.microsoft.com/zh-cn/library/cc903952 (V = vs.95). aspx