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

我们建议您执行上述代码并尝试更多资源(例如,背景颜色)。

广告
© . All rights reserved.