Muokkaa

Jaa


Application.Resources Property

Definition

Gets or sets a collection of application-scope resources, such as styles and brushes.

public:
 property System::Windows::ResourceDictionary ^ Resources { System::Windows::ResourceDictionary ^ get(); void set(System::Windows::ResourceDictionary ^ value); };
public System.Windows.ResourceDictionary Resources { get; set; }
[System.Windows.Markup.Ambient]
public System.Windows.ResourceDictionary Resources { get; set; }
member this.Resources : System.Windows.ResourceDictionary with get, set
[<System.Windows.Markup.Ambient>]
member this.Resources : System.Windows.ResourceDictionary with get, set
Public Property Resources As ResourceDictionary

Property Value

A ResourceDictionary object that contains zero or more application-scope resources.

Attributes

Examples

This example illustrates how to use XAML together with application-scope resources to create a consistent visual appearance. The first example is from App.xaml; the second, from MainWindow.xaml.

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml"
    >

  <Application.Resources>
    <SolidColorBrush x:Key="BackgroundColor" Color="Yellow"></SolidColorBrush>
  </Application.Resources>

</Application>
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ApplicationResourcesSnippetSample_XAML" 
    Height="300" 
    Width="300" 
    Background="{StaticResource BackgroundColor}"
    >
  <Grid>
    <!-- Additional XAML. -->
  </Grid>
</Window>

The following example shows how to set an application resource in code and XAML (in the App.xaml file in a C# WPF project or the Application.xaml file in a Visual Basic WPF project).

// Set an application-scope resource
Application.Current.Resources["ApplicationScopeResource"] = Brushes.White;
' Set an application-scope resource
Application.Current.Resources("ApplicationScopeResource") = Brushes.White
<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SDKSample.App"
    StartupUri="MainWindow.xaml" 
    Startup="App_Startup" 
    Exit="App_Exit">
    <Application.Resources>
        <SolidColorBrush x:Key="ApplicationScopeResource" Color="White"></SolidColorBrush>
    </Application.Resources>
</Application>

The following example shows how to get an application resource in code.

// Get an application-scope resource
Brush whiteBrush = (Brush)Application.Current.Resources["ApplicationScopeResource"];
' Get an application-scope resource
Dim whiteBrush As Brush = CType(Application.Current.Resources("ApplicationScopeResource"), Brush)

Remarks

The Resources property can be used to share resources across the windows and elements of an application. Additionally, the Resources property is included in the resource lookup path, which is traversed in the following order:

  1. Elements

  2. Windows

  3. Application.Resources

  4. System

Consequently, user interface (UI) elements can bind to application-scope resources. Additionally, if resources change, the resource system ensures that element properties which are bound to those resources are automatically updated to reflect the change.

Application-scope resources provide a simple way to support a consistent theme across your application. You can easily create a theme in XAML by using the Application.Resources tag. However, if your application supports multiple themes, which may contain a large number of theme elements, it might be easier to manage them using one ResourceDictionary instance for each theme. In this way, a new theme can be applied by setting the Resources property to the appropriate ResourceDictionary.

There are two considerations to make when using Resources. First, the dictionary key is an object, so you need to use exactly the same object instance when both setting and getting a property value (note that the key is case-sensitive when using a string). Second, the dictionary value is an object, so you will need to convert the value to the desired type when getting a property value.

Resources is thread safe and is available from any thread.

Applies to