WPF - 資源



資源通常是與某個物件關聯的定義,你預期會多次使用它。它能夠在本地儲存控制元件、當前視窗或整個應用程式的全域性資料。

將物件定義為資源允許我們從其他地方訪問它。這意味著該物件可以被重複使用。資源在資源字典中定義,任何物件都可以有效地定義為資源,使其成為可共享的資產。一個唯一的鍵指定給 XAML 資源,並使用該鍵,可以使用 StaticResource 標記擴充套件來引用它。

資源可以分為兩種型別:

  • StaticResource
  • DynamicResource

StaticResource 是一次性查詢,而 DynamicResource 更像資料繫結。它記住一個屬性與特定資源鍵關聯。如果與該鍵關聯的物件發生更改,動態資源將更新目標屬性。

示例

這是一個簡單的 SolidColorBrush 資源應用程式。

  • 讓我們建立一個名為 **WPFResouces** 的新 WPF 專案。

  • 拖動兩個矩形,並按以下 XAML 程式碼所示設定它們的屬性。

<Window x:Class = "WPFResources.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:WPFResources" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "525"> 
	
   <Window.Resources> 
      <SolidColorBrush x:Key = "brushResource" Color = "Blue" /> 
   </Window.Resources> 
	
   <StackPanel> 
      <Rectangle Height = "50" Margin = "20" Fill = "{StaticResource brushResource}" /> 
      <Rectangle Height = "50" Margin = "20" Fill = "{DynamicResource brushResource}" /> 
      <Button x:Name = "changeResourceButton"
         Content = "_Change Resource" Click = "changeResourceButton_Click" /> 
   </StackPanel> 
	
</Window> 
  • 在上面的 XAML 程式碼中,您可以看到一個矩形具有 StaticResource,另一個具有 DynamicResource,brushResource 的顏色為 Bisque(米色)。

  • 編譯並執行程式碼後,它將生成以下 MainWindow。

MainWindow of Resources

單擊“更改資源”按鈕時,您將看到具有 DynamicResource 的矩形會將其顏色更改為紅色。

Change Resources

資源範圍

資源定義在 **資源字典** 中,但是可以定義資源字典的許多地方。在上面的示例中,資源字典是在視窗/頁面級別定義的。資源在哪個字典中定義會立即限制該資源的範圍。因此,範圍(即可以使用資源的位置)取決於你定義它的位置。

  • 在網格的資源字典中定義資源,則只有該網格及其子元素可以訪問它。

  • 在視窗/頁面上定義它,則該視窗/頁面上的所有元素都可以訪問它。

  • 應用程式根目錄可以在 App.xaml 資源字典中找到。它是我們應用程式的根目錄,因此此處定義的資源的範圍是整個應用程式。

就資源的範圍而言,最常見的是應用程式級別、頁面級別和特定元素級別(如 Grid、StackPanel 等)。

Resource Scope

上面的應用程式在其視窗/頁面級別具有資源。

資源字典

XAML 應用程式中的資源字典意味著資源字典儲存在單獨的檔案中。這幾乎在所有 XAML 應用程式中都遵循。在單獨的檔案中定義資源具有以下優點:

  • 在資源字典和 UI 相關程式碼之間分離。

  • 在單獨的檔案(如 App.xaml)中定義所有資源,將使它們在整個應用程式中可用。

那麼,我們如何在單獨的檔案中在資源字典中定義我們的資源呢?這很容易,只需按照以下步驟透過 Visual Studio 新增新的資源字典:

  • 在你的解決方案中,新增一個新資料夾並將其命名為 **ResourceDictionaries**。

  • 右鍵單擊此資料夾,然後從“新增”子選單項中選擇“資源字典”,並將其命名為 **DictionaryWithBrush.xaml**

示例

現在讓我們採用相同的示例,但是在這裡,我們將在應用程式級別定義資源字典。MainWindow.xaml 的 XAML 程式碼如下:

<Window x:Class = "WPFResources.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:WPFResources" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "525"> 
	
   <StackPanel> 
      <Rectangle Height = "50" Margin = "20" Fill = "{StaticResource brushResource}" /> 
      <Rectangle Height = "50" Margin = "20" Fill = "{DynamicResource brushResource}" /> 
      <Button x:Name = "changeResourceButton"
         Content = "_Change Resource" Click = "changeResourceButton_Click" /> 
   </StackPanel> 
	
</Window>

這是 DictionaryWithBrush.xaml 中的實現:

<ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"> 
	
   <SolidColorBrush x:Key = "brushResource" Color = "Blue" /> 
</ResourceDictionary> 

這是 app.xaml 中的實現:

<Application x:Class="WPFResources.App" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   StartupUri = "MainWindow.xaml"> 
	
   <Application.Resources> 
      <ResourceDictionary Source = " XAMLResources\ResourceDictionaries\DictionaryWithBrush.xaml"/> 
   </Application.Resources> 
	
</Application> 

編譯並執行上述程式碼後,將產生以下輸出:

Resource Dictionaries Output

單擊“更改資源”按鈕時,矩形會將其顏色更改為紅色。

Change Resource Dictionaries

我們建議您執行上述程式碼並嘗試更多資源(例如,背景顏色)。

廣告