如何在WPF中使用全局资源?

时间:2022-01-02 17:14:30

I have a WPF application which I would like to use some static resources in. I have created a Resource Library XAML file which contains a resource. I have also added a string into the Resources of the project through the Properties panel.

我有一个WPF应用程序,我想使用一些静态资源。我创建了一个包含资源的资源库XAML文件。我还通过Properties面板在项目的Resources中添加了一个字符串。

I assumed I could just use these resources with the binding expression:

我假设我可以将这些资源与绑定表达式一起使用:

{StaticResource ResourceName}

But visual studio is telling me the resources are not found. Do I have to include some form of reference in my XAML? The examples I have seen only include resources locally such as:

但是visual studio告诉我没有找到资源。我是否必须在我的XAML中包含某种形式的参考?我见过的例子只包括本地资源,例如:

<Window.Resources>, <Page.Resources> etc

I don't want to include the resources locally because I want them to be available to multiple parts of the application.

我不想在本地包含资源,因为我希望它们可用于应用程序的多个部分。

4 个解决方案

#1


9  

Put them in the App.xaml :)

把它们放在App.xaml :)

In each WPF application there is an App.xaml (and its corresponding code file, App.xaml.cs or App.xaml.vb) which works as a global file for application wide declarations. You can use this file to declare a constant look and feel across your application. For example you can declare a default red background for all buttons in an application.

在每个WPF应用程序中都有一个App.xaml(及其相应的代码文件App.xaml.cs或App.xaml.vb),它作为应用程序范围声明的全局文件。您可以使用此文件在应用程序中声明持久的外观。例如,您可以为应用程序中的所有按钮声明默认的红色背景。

http://www.microsoft.com/emea/msdn/thepanel/en/articles/introduction_wpf.aspx

http://www.microsoft.com/emea/msdn/thepanel/en/articles/introduction_wpf.aspx

#2


2  

Look at the section on "static resource lookup behavior" here. Anything in the application's resource dictionary (i.e. Application.Resources) is available globally. Whenever you look up a resource with a given key, and the key isn't used anywhere in the hierarchy of local resource dictionaries, the one in the application's dictionary will be returned.

请在此处查看“静态资源查找行为”部分。应用程序资源字典中的任何内容(即Application.Resources)都可在全球范围内使用。每当您使用给定密钥查找资源,并且密钥未在本地资源字典的层次结构中的任何位置使用时,将返回应用程序字典中的密钥。

To populate the application's resource dictionary in XAML, find the XAML file for the application (usually App.xaml) and add an Application.Resources element.

要在XAML中填充应用程序的资源字典,请查找应用程序的XAML文件(通常为App.xaml)并添加Application.Resources元素。

#3


2  

You can create a dictionary of global resources and include its path in app.xaml

您可以创建全局资源的字典,并在app.xaml中包含其路径

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/YourProject;Component/YourXamlFile.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Now all your resources in the YourXamlFile.xaml will be visible globally in your project.

现在,YourXamlFile.xaml中的所有资源都将在项目中全局显示。

#4


0  

Just to add my 2 cents here.

只是在这里加2美分。

I also wanted to use global resources, but I use a custom Application class, so I couldn't just put it in the App.xaml.

我也想使用全局资源,但我使用自定义的Application类,所以我不能把它放在App.xaml中。

I created a Resources.xaml in my root location, and then did this:

我在我的根位置创建了一个Resources.xaml,然后执行了以下操作:

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/MyWpfClient;component/Resources.xaml", UriKind.Relative) });

But be warned, the Resource.xaml MUST have the Build Action to 'Page'. When it didn't it couldn't find some types I used as converters.

但是请注意,Resource.xaml必须将Build Action设置为'Page'。如果没有,它找不到我用作转换器的某些类型。

#1


9  

Put them in the App.xaml :)

把它们放在App.xaml :)

In each WPF application there is an App.xaml (and its corresponding code file, App.xaml.cs or App.xaml.vb) which works as a global file for application wide declarations. You can use this file to declare a constant look and feel across your application. For example you can declare a default red background for all buttons in an application.

在每个WPF应用程序中都有一个App.xaml(及其相应的代码文件App.xaml.cs或App.xaml.vb),它作为应用程序范围声明的全局文件。您可以使用此文件在应用程序中声明持久的外观。例如,您可以为应用程序中的所有按钮声明默认的红色背景。

http://www.microsoft.com/emea/msdn/thepanel/en/articles/introduction_wpf.aspx

http://www.microsoft.com/emea/msdn/thepanel/en/articles/introduction_wpf.aspx

#2


2  

Look at the section on "static resource lookup behavior" here. Anything in the application's resource dictionary (i.e. Application.Resources) is available globally. Whenever you look up a resource with a given key, and the key isn't used anywhere in the hierarchy of local resource dictionaries, the one in the application's dictionary will be returned.

请在此处查看“静态资源查找行为”部分。应用程序资源字典中的任何内容(即Application.Resources)都可在全球范围内使用。每当您使用给定密钥查找资源,并且密钥未在本地资源字典的层次结构中的任何位置使用时,将返回应用程序字典中的密钥。

To populate the application's resource dictionary in XAML, find the XAML file for the application (usually App.xaml) and add an Application.Resources element.

要在XAML中填充应用程序的资源字典,请查找应用程序的XAML文件(通常为App.xaml)并添加Application.Resources元素。

#3


2  

You can create a dictionary of global resources and include its path in app.xaml

您可以创建全局资源的字典,并在app.xaml中包含其路径

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/YourProject;Component/YourXamlFile.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Now all your resources in the YourXamlFile.xaml will be visible globally in your project.

现在,YourXamlFile.xaml中的所有资源都将在项目中全局显示。

#4


0  

Just to add my 2 cents here.

只是在这里加2美分。

I also wanted to use global resources, but I use a custom Application class, so I couldn't just put it in the App.xaml.

我也想使用全局资源,但我使用自定义的Application类,所以我不能把它放在App.xaml中。

I created a Resources.xaml in my root location, and then did this:

我在我的根位置创建了一个Resources.xaml,然后执行了以下操作:

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/MyWpfClient;component/Resources.xaml", UriKind.Relative) });

But be warned, the Resource.xaml MUST have the Build Action to 'Page'. When it didn't it couldn't find some types I used as converters.

但是请注意,Resource.xaml必须将Build Action设置为'Page'。如果没有,它找不到我用作转换器的某些类型。