Application.Resources Property

Gets a collection of application-scoped resources, such as styles, templates, and brushes.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public ResourceDictionary Resources { get; set; }
<Application>
  <Application.Resources>
    oneOrMoreResourceElements
  </Application.Resources>
</Application>

XAML Values

oneOrMoreResourceElements

One or more object elements, each of which creates and defines a resource. Each resource property element within each ResourceDictionary must have a unique value for the x:Key, which serves as the unique key when values are retrieved from the ResourceDictionary.

Property Value

Type: System.Windows.ResourceDictionary
A ResourceDictionary object that contains zero or more application-scoped resources.

The value of the Resources property contains the ResourceDictionary that is set from XAML by using the Application.Resources property element.

The following example shows a style (textBlockStyle) declared as an application-scoped resource that is shared across multiple UIs.

First, the resource is implemented in the markup for the application class, as shown here.


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

    <!--Application Resources-->
    <Application.Resources>
        <Style x:Key="textBlockStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService 
            Launching="Application_Launching" Closing="Application_Closing" 
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>


The following UIs demonstrate the application-scoped textBlockStyle being shared.


<phone:PhoneApplicationPage 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:local="clr-namespace:WindowsPhoneApp1"
    x:Class="WindowsPhoneApp1.Page" mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    d:DesignHeight="800" d:DesignWidth="480">

    <StackPanel>
        <TextBlock Style="{StaticResource textBlockStyle}">
            This text is red!
        </TextBlock>
        <local:AnotherPage />
    </StackPanel>

</phone:PhoneApplicationPage>



<phone:PhoneApplicationPage 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    x:Class="WindowsPhoneApp1.AnotherPage" mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    d:DesignHeight="800" d:DesignWidth="480">

    <StackPanel>
        <TextBlock Style="{StaticResource textBlockStyle}">
            This text is red too!
        </TextBlock>
    </StackPanel>

</phone:PhoneApplicationPage>


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft