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. xamlThe 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 專案或 Visual Basic WPF 專案) 中的 app.xaml 檔中,以程式碼和 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) 元素可以系結至應用程式範圍的資源。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. 您可以使用標記,輕鬆地在 XAML 中建立主題 Application.ResourcesYou can easily create a theme in XAML 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. 如此一來,就可以藉由將 Resources 屬性設定為適當的方式來套用新的主題 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.

適用於