This is a mirror of official site: http://jasper-net.blogspot.com/

Creating and Consuming Resource Dictionaries in WPF and Silverlight

| Sunday, June 6, 2010
Resource Dictionaries are a powerful feature in WPF and Silverlight that enable developers to organize and consume reusable resources.

After completing this walk through you'll understand how to create and merge resource dictionaries in WPF and Silverlight; how to add, consume and modify resources in the dictionary using the tools provided by the WPF and Silverlight Designer in Visual Studio 2010.

Table of Contents
What is a Resource?
Where Can I Locate Resources?
How Do I Create Resources Similar To The Example?
Resources are Declared at Different Scopes
Show me how a ResourceDictionary Used
Where to Go From Here
Links
Comments

What is a Resource?

Before diving into resource dictionaries let's first understand what the term "resource" means when used in the context of a resource dictionary or in a resources section of an object in XAML.

A resource is an object declared in XAML. In the below XAML we see examples of typical resources; my: is an alias for the System namespace in mscorlib.

<UserControl.Resources>
   <LinearGradientBrush x:Key="formBackground" EndPoint="0.5,1" StartPoint="0.5,0">
       <GradientStop Color="Blue" Offset="0" />
       <GradientStop Color="#460000FF" Offset="1" />
   </LinearGradientBrush>
   <my:String x:Key="applicationTitle">Resource Dictionary Demo</my:String>
   <my:Double x:Key="applicationTitleFontSize">18</my:Double>
   <SolidColorBrush x:Key="applicationTitleForeground">Yellow</SolidColorBrush>
</UserControl.Resources>
One reason to use resources in your applications is to promote object reuse across your application. Object reuse provides consistency across the application. Object reuse also makes it very easy to change the application; since you only need to change the resources and all consumers will pick up the change.

The below XAML consumes the above resources.

<Grid x:Name="LayoutRoot" Background="{StaticResource formBackground}">
   <TextBlock
       Text="{StaticResource applicationTitle}"
       Foreground="{StaticResource applicationTitleForeground}"
       FontSize="{StaticResource applicationTitleFontSize}"
       VerticalAlignment="Top"/>
</Grid>

Where Can I Locate Resources?

Resources are located in resource dictionaries.


Read more: WPF & Silverlight Designer

Posted via email from jasper22's posterous

0 comments: