Application.Resources 屬性

定義

取得或設定應用程式範圍的資源集合,例如樣式和筆刷。

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

屬性值

ResourceDictionary

ResourceDictionary 物件,包含零個以上的應用程式範圍資源。

屬性

範例

此範例說明如何使用 XAML 搭配應用程式範圍資源來建立一致的視覺外觀。 第一個範例來自 App.xaml;第二個,來自 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.xaml 檔案中,或在 Visual Basic WPF 專案) 的 App.xaml 檔案中設定應用程式資源和 XAML (。

// 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>

下列範例示範如何在程式碼中取得應用程式資源。

// 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 可用來跨應用程式的視窗和元素共用資源。 此外, Resources 屬性會包含在資源查閱路徑中,依下列順序周遊:

  1. 元素

  2. Windows

  3. Application.Resources

  4. 系統

因此,使用者介面 (UI) 元素可以系結至應用程式範圍資源。 此外,如果資源變更,資源系統可確保系結至這些資源的專案屬性會自動更新,以反映變更。

應用程式範圍資源提供簡單的方式來支援整個應用程式一致的主題。 您可以使用 標籤,輕鬆地在 XAML Application.Resources 中建立主題。 不過,如果您的應用程式支援多個主題,其中可能包含大量的主題元素,使用每個主題的一個 ResourceDictionary 實例來管理它們可能會比較容易。 如此一來,可以將 Resources 屬性設定為適當的 ResourceDictionary 來套用新的主題。

使用 Resources 時,有兩個考慮。 首先,字典 索引鍵 是物件,因此當您設定和取得屬性值時,都必須使用完全相同的物件實例, (請注意,使用字串) 時,索引鍵會區分大小寫。 其次,字典 是物件,因此在取得屬性值時,您必須將值轉換為所需的類型。

Resources 是安全線程,可從任何執行緒取得。

適用於