Using Resources in XAML
You can also use resources in XAML directly. In fact, it is common to do so! This is made possible by something called the StaticResource markup extension.
WARNING
Windows Presentation Foundation, which is Silverlight's "big sister," allows storing resources in external files and even external assemblies. This creates an even better separation.
This is not possible in Silverlight yet, however, and all the resources must be stored in the same XAML file as the controls using them, or in the global file App.xaml.
346 CHAPTER 17 Using Resources, Styling, and Templating
Markup Extensions
These extensions are used to add functionality to the XAML markup. Silverlight has four of them built in:
► StaticResource—Used to refer to a resource stored in the same XAML file or in App.xaml
► Binding—Used in data binding, and which we study in Chapter 18, "Data Binding and Using Data Controls"
► TemplateBinding—Used in the creation of templates, which we learn about later in this chapter
► x:Null—Used when you need to set a property to null in XAML
Markup extensions in XAML are used within curly brackets: <Rectangle Fill="{StaticResource MyBrush}" Height="20"/>
Walking the Tree
The StaticResource markup extension has a neat feature: It walks the tree of elements trying to find the right resource. For example, the XAML markup in Listing 17.4 works:
LISTING 17.4 Walking the Tree
<Grid x:Name="LayoutRoot"> <Grid.Resources>
<SolidColorBrush x:Key="BackgroundBrush" Color="Red" /> </Grid.Resources>
<StackPanel>
<StackPanel.Resources>
<SolidColorBrush x:Key="ForegroundBrush" Color="Blue" /> </StackPanel.Resources>
<TextBox Text="Hello"
Foreground="{StaticResource ForegroundBrush}" Background="{StaticResource BackgroundBrush}" /> </StackPanel> </Grid>
Even though the ForegroundBrush is defined in the TextBox's parent, and the BackgroundBrush in its parent's parent, the brushes are found because of the "tree walking." Note however that the tree can only be walked upwards, not downwards. The StaticResource markup extension cannot use forward references. For example, trying to run the markup in Listing 17.5 causes an exception to be thrown. The issue is that when
Using ResourceDictionaries in Silverlight 347
the StaticResource is used, the corresponding resource has not been parsed yet by the XAML parser.
LISTING 17.5 Undefined Resource
<TextBox Text="Hello"
Foreground="{StaticResource MyOwnBrush}"> <TextBox.Resources>
<SolidColorBrush x:Key="MyOwnBrush" Color="Orange" /> </TextBox.Resources> </TextBox>
To solve this problem, you can use the syntax in Listing 17.6:
LISTING 17.6 Correcting the Error
<TextBox Text="Hello"> <TextBox.Resources>
<SolidColorBrush x:Key=MMyOwnBrush" Color=MOrange" /> </TextBox.Resources> <TextBox.Foreground>
<StaticResource ResourceKey="MyOwnBrushM/> </TextBox.Foreground> </TextBox>
For the moment there is no equivalent to StaticResource and its "tree-walking" in code-behind in Silverlight. Only the local resources of an element can be read using the element's Resources property. However, implementing the equivalent of the TryFindResource method (existing in Windows Presentation Foundation) is not difficult. We see how to do this in Chapter 24, "Silverlight: Continuing the Journey."
Post a comment