Resources
Resources provide your XAML with a way to define and share objects. You can share resources at the page (window) level, or throughout an entire application (or even across an entire system!).
The Resources section of your XAML is sometimes referred to as the "resource dictionary." To create a resource that is scoped to a single window, you use this element:
<Window.Resources>
as shown in Example 3-8.
Example 3-8. A static resource scoped to a single window
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Programming .NET 3.5 | Resources">
<Window.Resources>
<SolidColorBrush x:Key="GreenBrush" Color="Green" />
</Window.Resources>
<Viewbox>
<TextBlock Foreground="{StaticResource GreenBrush}">
Little Green Men
</TextBlock>
</Viewbox>
</Window>The resource is applied to a TextBlock by setting the foreground value to the resource itself. The keyword StaticResource indicates that the resource will be set at compile time and cannot be updated by associating a different resource with the resource key at runtime. In contrast, dynamic resources can usually be defined at runtime. If you make all your skinnable properties DynamicResource references, you will be able to change the skin just by dropping in a different resource dictionary. WPF will automatically update all the properties when you do this.
The result of applying the GreenBrush resource to the TextBlock is shown in Figure 3-14.
Figure 3-14. A ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access