Windows Phone 7 defines and uses dictionary resources (resourcedictionary)

Source: Internet
Author: User

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

Related Article

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.