Wpf 简明教程

WPF - Resources

资源通常与某个对象相关的定义,而您期望对该对象重复使用。它具有将数据存储在控件或当前窗口中(本地存储)或针对整个应用程序进行全局存储的能力。

Resources are normally definitions connected with some object that you just anticipate to use more often than once. It is the ability to store data locally for controls or for the current window or globally for the entire applications.

将某个对象定义为资源允许我们从其他地方访问该对象。这意味着该对象可以重用。资源在资源词典中进行定义,任何对象都可以定义为资源,从而有效地使其成为可共享资产。唯一的键指定给 XAML 资源,通过此键可以利用 StaticResource 标记扩展进行引用。

Defining an object as a resource allows us to access it from another place. What it means is that the object can be reused. Resources are defined in resource dictionaries and any object can be defined as a resource effectively making it a shareable asset. A unique key is specified to an XAML resource and with that key, it can be referenced by using a StaticResource markup extension.

资源可以分为两种类型 −

Resources can be of two types −

  1. StaticResource

  2. DynamicResource

StaticResource(静态资源)是一次性查找,而 DynamicResource(动态资源)的工作方式更加类似于数据绑定。它记住某个属性与特定的资源键相关联。如果与该键关联的对象发生更改,动态资源将更新目标属性。

A StaticResource is a onetime lookup, whereas a DynamicResource works more like a data binding. It remembers that a property is associated with a particular resource key. If the object associated with that key changes, dynamic resource will update the target property.

Example

以下是一个 SolidColorBrush 资源的简单应用程序。

Here’s a simple application for the SolidColorBrush resource.

  1. Let’s create a new WPF project with the name WPFResouces.

  2. Drag two Rectangles and set their properties as shown in the following XAML code.

<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>
  1. In the above XAML code, you can see that one rectangle has StaticResource and the other one has DynamicResource and the color of brushResource is Bisque.

  2. When you compile and execute the code, it will produce the following MainWindow.

main window of resources

当您单击“更改资源”按钮时,您将看到具有 DynamicResource 的矩形变为红色。

When you click the "Change Resource" button, you will see that the rectangle with DynamicResource will change its color to Red.

change resources

Resource Scope

资源在 resource dictionaries 中进行定义,但可以在许多地方定义资源词典。在上述示例中,资源词典在 Window/page 级别上得到定义。资源在哪个词典中得到定义当即限制了该资源的作用域。因此作用域(即您可以在哪里使用资源)取决于您在何处对其进行定义。

Resources are defined in resource dictionaries, but there are numerous places where a resource dictionary can be defined. In the above example, a resource dictionary is defined on Window/page level. In what dictionary a resource is defined immediately limits the scope of that resource. So the scope, i.e. where you can use the resource, depends on where you’ve defined it.

  1. Define the resource in the resource dictionary of a grid and it’s accessible by that grid and by its child elements only.

  2. Define it on a window/page and it’s accessible by all elements on that window/page.

  3. The app root can be found in App.xaml resources dictionary. It’s the root of our application, so the resources defined here are scoped to the entire application.

就资源的范围而言,最常见的是应用程序级别、页面级别和特定的元素级别,如 Grid、StackPanel 等。

As far as the scope of the resource is concerned, the most often are application level, page level, and a specific element level like a Grid, StackPanel, etc.

resource scope

上面的应用程序在其 Window/page 级别中有资源。

The above application has resources in its Window/page level.

Resource Dictionaries

XAML 应用程序中的资源词典意味着资源词典保存在单独的文件中。几乎所有 XAML 应用程序中都遵循此做法。在单独的文件中定义资源可能具有以下优点 -

Resource dictionaries in XAML apps imply that the resource dictionaries are kept in separate files. It is followed in almost all XAML apps. Defining resources in separate files can have the following advantages −

  1. Separation between defining resources in the resource dictionary and UI related code.

  2. Defining all the resources in a separate file such as App.xaml would make them available across the app.

那么,如何在单独的文件中的资源词典中定义我们的资源?很简单,只需通过 Visual Studio 按照以下给定的步骤添加一个新资源词典 -

So, how do we define our resources in a resource dictionary in a separate file? Well, it is very easy, just add a new resource dictionary through Visual Studio by following steps given below −

  1. In your solution, add a new folder and name it ResourceDictionaries.

  2. Right-click on this folder and select Resource Dictionary from Add submenu item and name it DictionaryWithBrush.xaml

Example

让我们现在采用相同的示例,但在这里,我们将定义应用程序级别的资源词典。MainWindow.xaml 的 XAML 代码如下 -

Let’s now take the same example, but here, we will define the resource dictionary in app level. The XAML code for MainWindow.xaml is as follows −

<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 中的实现 -

Here is the implementation in 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 中的实现 -

Here is the implementation in 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>

编译并执行上述代码时,将产生以下输出 -

When the above code is compiled and executed, it will produce the following output −

resource dictionaries output

当你单击“更改资源”按钮时,矩形将变为红色。

When you click the Change Resource button, the rectangle will change its color to Red.

change resource dictionaries

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

We recommend that you execute the above code and try some more resources (for example, background color).