Friday, May 4, 2007

Resources In WPF

we can reuse objects in XAML by first defining them as Resources .. this is not a .net normal resources. Because that will in the resources file and we can use for storing images , text , etc... and accessed with Uri object. and also it will shipped with executable files or .dll files. Here we are going to write resources inside of XAML or (sometimes in C# code). It will associated with an element or control, page or Window in the application.

Format of Resources:

<SomeType x:Key=”keyname” ...> </SomeType>

Type of Resources :
  • Static Resources
  • Dynamic Resources

Static Resources

XAML elements can reference the resources using the key using markup extension. The particular markup extension you use with resources is named StaticResource.

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib">
<StackPanel.Resources>
<s:Double x:Key="fontsizeLarge">18.7 </s:Double>
<s:Double x:Key="fontsizeSmall"> 14.7 </s:Double>
</StackPanel.Resources>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="24">
<Button.FontSize>
<StaticResource ResourceKey="fontsizeLarge" />
</Button.FontSize>
Button with large FontSize
</Button>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="24" FontSize="{StaticResource fontsizeSmall}" >
Button with small FontSize </Button>
</StackPanel>


Here fontsizeSmall and fontsizeLarge is the static resources. And we have created that resources in element stackpanel. So we can use that resources only child of stackpanel. In case if we declared that resources in window section means we can use anywhere in the single source code. That means insidea of window element.method of accessing static resources .


<Button.FontSize> <StaticResource ResourceKey=”fontsizeLarge”>
</Button.FontSize>
(or)
FontSize=”{StaticResource fontsizeLarge}”

Note:

Forward reference is not allowed in resource reference.We can define control or element as a resource. But we cant use twice. because it will create only one instance.

Creating resources using C# code.
elementname.Resource.Add(“resource name”,value);

Finding Resources
Application.Current.FindResource(key);

Application Resources

we can define resources in the Application file . And we can access those resources from anywhere in the application. For that we need create resources under MyApp.xaml file which is created by the VisulaStudio.

<Application.Resources>
</Application.Resources>

XAML defines a markup extension named x:Static specifically to reference static properties of fields.It also works with enumeration members.

Example :
some static property from a class named MyClass.

<Button.Content>
<x:Static Member=”MyClass:PropertyName” />
</Button.Content>


Dynamic Resource

With StaticResource the key is used to access the object once and the object is retained . When we use the DynamicResource the key is retained and the object is accessed when its needed. The primary purpose of DynamicResource is to access system resources such as colors.

Resources:
if we want share multiple resources among multiple applications. We can use resource dictionary.

Example: SampleResource1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">;
<LinearGradientBrush x:Key="brushLinear">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Pink" Offset="0" />
<GradientStop Color="Aqua" Offset="1" />
</LinearGradientBrush.GradientStops> </LinearGradientBrush>
</ResourceDictionary>

SampleResource2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">;
<RadialGradientBrush x:Key="brushRadial"> <RadialGradientBrush.GradientStops>
<GradientStop Color="Pink" Offset="0" />
<GradientStop Color="Aqua" Offset="1" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</ResourceDictionary>

MyApp.xaml(Application file)

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" StartupUri="UseCommonResourcesWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources1.xaml" />
<ResourceDictionary Source="MyResources2.xaml" /> </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

we can access the resources like this .

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Title="Use Common Resources" Background="{StaticResource brushLinear}">
<Button FontSize="96pt" HorizontalAlignment="Center" VerticalAlignment="Center" Background="{StaticResource brushRadial}">
Button
</Button>
</Window>

Enjoy!!!!!!!!!! If you have a question or you cant get anything in this feel free to comment. I will try to give you as soon as possible.

6 comments:

  1. Just wondering, is there any way to attach a resourcedictionary to a Class Library, just like you can do to the App.XAML

    The purpose is that I do not wan't to add

    UserControl.Resources
    ResourceDictionary Source="ResDictionary.xaml"
    etc.

    to each user control.

    Thanks in advance.

    ReplyDelete
  2. i dont want to add styles to my xaml windows i want to apply some background at app.xaml. so that all windows including new one also acuire the same behaviour.

    how to do that?

    ReplyDelete
  3. If resource dictionaries merging is done from code, do we need to use DynamicResource markup extension to use any resource from the merged dictionary or can we use Static Resource also?

    ReplyDelete