James Jeffery
James Jeffery

Reputation: 557

Application Level Resources XAML

How do I create an application level resources in XAML? I'm developing a Windows Phone 8 app btw.

Below I have a rectangle, I want to create a resources that can be used to change the colour of the rectangle when tapped:

<Rectangle Fill="#FFF4F4F5" 
           HorizontalAlignment="Left" 
           Height="100" 
           Stroke="Black"
           VerticalAlignment="Top"
           Width="100"
           x:Name="pad1"
           Tap="pad1_tap"           
 />

I have read some similar posts that say to use:

<Application.Resources>
    <!-- Resources Here !-->
</Application.Resources>

... but there is no object under the name 'Application' within my application. When trying to use 'Application.Resources' I get an error stating: The member Resources is not recognized or accesssible.

Upvotes: 1

Views: 3025

Answers (2)

Kevin Gosse
Kevin Gosse

Reputation: 39007

The Application object is in your app.xaml file. But it's useful only if you want your resource to be shared by the whole application. If you need it only in one page, you can declare your resource in the PhoneApplicationPage element:

<phone:PhoneApplicationPage.Resources>
    <!-- your resource -->
</phone:PhoneApplicationPage.Resources>

Upvotes: 4

src091
src091

Reputation: 2847

You'd need to import a mscorlib library and then refer to your resources using StaticResource keyword, like so:

<Application
    x:Class="AppClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <Application.Resources>
        <sys:Int32 x:Key="Test">80</sys:Int32>
    </Application.Resources>
</Application>

Usage:

<Rectangle Fill="#FFF4F4F5" 
           HorizontalAlignment="Left" 
           Height="100" 
           Stroke="Black"
           VerticalAlignment="Top"
           Width="{StaticResource Test}"
           x:Name="pad1"
           Tap="pad1_tap"           
 />

Upvotes: 0

Related Questions