Application.Resources 属性

定义

获取或设置应用程序范围资源(如样式和画笔)的集合。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); };
[System.Windows.Markup.Ambient]
public System.Windows.ResourceDictionary Resources { get; set; }
public System.Windows.ResourceDictionary Resources { get; set; }
[<System.Windows.Markup.Ambient>]
member this.Resources : System.Windows.ResourceDictionary with get, set
member this.Resources : System.Windows.ResourceDictionary with get, set
Public Property Resources As ResourceDictionary

属性值

ResourceDictionary

一个包含零个或多个应用程序范围资源的 ResourceDictionary 对象。A ResourceDictionary object that contains zero or more application-scope resources.

属性

示例

此示例演示如何将 XAML 与应用程序范围资源一起使用来创建一致的视觉外观。This example illustrates how to use XAML together with application-scope resources to create a consistent visual appearance. 第一个示例来自 app.xaml;第二个(来自 mainwindow.xaml)。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>

下面的示例演示如何在 c # WPF 项目中的 App.config 文件中或在 Visual Basic WPF) 项目中的应用程序 .xaml 文件中设置应用程序资源 (。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)

注解

Resources属性可用于跨应用程序的窗口和元素共享资源。The Resources property can be used to share resources across the windows and elements of an application. 此外,该 Resources 属性包含在资源查找路径中,按以下顺序遍历:Additionally, the Resources property is included in the resource lookup path, which is traversed in the following order:

  1. 元素Elements

  2. WindowsWindows

  3. Application.Resources

  4. 系统System

因此, 用户界面 (UI)user interface (UI) 元素可以绑定到应用程序范围的资源。Consequently, 用户界面 (UI)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. 通过使用标记,可以在中轻松创建主题 XAMLXAML Application.ResourcesYou can easily create a theme in XAMLXAML by using the Application.Resources tag. 但是,如果你的应用程序支持多个主题,其中可能包含大量主题元素,则使用 ResourceDictionary 每个主题的一个实例来管理它们可能会更容易。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. 通过这种方式,可以通过将资源属性设置为相应的来应用新的主题 ResourceDictionaryIn this way, a new theme can be applied by setting the Resources property to the appropriate ResourceDictionary.

使用时有两个注意事项 ResourcesThere 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 是线程安全的,可从任何线程获得。Resources is thread safe and is available from any thread.

适用于