15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

styles <strong>and</strong> resources ❘ 1007<br />

}<br />

{<br />

new GradientStop(Colors.White, 0.0),<br />

new GradientStop(Colors.Yellow, 0.14),<br />

new GradientStop(Colors.YellowGreen, 0.7)<br />

};<br />

myContainer.Resources.Add("MyGradientBrush", brush);<br />

code snippet StylesAndResources/ResourceDemo.xaml .cs<br />

When running the application, the resource changes dynamically by clicking the Change Resource button.<br />

Using the button with dynamic resource gets the dynamically created resource; the button with static<br />

resource looks the same as before.<br />

DynamicResource requires more performance than StaticResource because the<br />

resource is always loaded when needed. Use DynamicResource only with resources<br />

where you expect changes during runtime.<br />

resource dictionaries<br />

If the same resources are used with different applications, it ’ s useful to put the resource in a resource<br />

dictionary. Using resource dictionaries, the fi les can be shared between multiple applications, or the<br />

resource dictionary can be put into an assembly <strong>and</strong> shared by the applications.<br />

To share a resource dictionary in an assembly, create a library. A resource dictionary fi le, here<br />

Dictionary1.xaml , can be added to the assembly. The build action for this fi le must be set to Resource so<br />

that it is added as a resource to the assembly.<br />

Dictionary1.xaml defi nes two resources: LinearGradientBrush with the CyanGradientBrush key, <strong>and</strong> a<br />

style for a Button that can be referenced with the PinkButtonStyle key:<br />

< ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ><br />

< LinearGradientBrush x:Key="CyanGradientBrush" StartPoint="0,0" EndPoint="0.3,1" ><br />

< GradientStop Offset="0.0" Color="LightCyan" / ><br />

< GradientStop Offset="0.14" Color="Cyan" / ><br />

< GradientStop Offset="0.7" Color="DarkCyan" / ><br />

< /LinearGradientBrush ><br />

< Style x:Key="PinkButtonStyle" TargetType="Button" ><br />

< Setter Property="FontSize" Value="22" / ><br />

< Setter Property="Foreground" Value="White" / ><br />

< Setter Property="Background" ><br />

< Setter.Value ><br />

< LinearGradientBrush StartPoint="0,0" EndPoint="0,1" ><br />

< GradientStop Offset="0.0" Color="Pink" / ><br />

< GradientStop Offset="0.3" Color="DeepPink" / ><br />

< GradientStop Offset="0.9" Color="DarkOrchid" / ><br />

< /LinearGradientBrush ><br />

< /Setter.Value ><br />

< /Setter ><br />

< /Style ><br />

< /ResourceDictionary ><br />

code snippet ResourcesLib/Dictionary1.xaml<br />

With the target project, the library needs to be referenced, <strong>and</strong> the resource dictionary added to the dictionaries.<br />

You can use multiple resource dictionary fi les that can be added with the MergedDictionaries property of the<br />

ResourceDictionary . A list of resource dictionaries can be added to the merged dictionaries. With the Source<br />

property of ResourceDictionary , a dictionary can be referenced. For the reference, the pack URI syntax is<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!