WPF中如何使用全球资源?如何使用、全球、资源、WPF

2023-09-04 08:11:05 作者:油炸小可爱

我有一个WPF应用程序,我想用一些静态的资源。我创建了一个资源库的XAML文件,其中包含的资源。我也通过属性面板中添加一个字符串到项目的资源。

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.

我以为我可以只使用这些资源,结合前pression:

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

{StaticResource ResourceName}

但是,Visual Studio是告诉我的资源都没有发现。我一定要包括某种形式的在我的XAML参考?我只看到这些例子包括本地资源,如:

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:

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

推荐答案

,把它们放进App.xaml中:)

Put them in the App.xaml :)

在每一个WPF应用程序中有一个   的App.xaml(及其相应code   文件App.xaml.cs或App.xaml.vb)   其中工程作为一个全球性的文件   应用广泛的声明。您可以   使用此文件来声明一个常量   外观和感觉在你的应用程序。   例如,你可以声明默认   红色背景中的所有按钮   应用程序。

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.

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