WPF 共享资源问题 - 没有 App.xaml,没有共享资源
我在我正在开发的应用程序中遇到了一个小(很大)问题。
我正在为我的公司开发一个应用程序模块。该应用程序是一个 WinForm 应用程序,但我一直在开发一个 WPF 应用程序(实际上并不是您将看到的应用程序),该应用程序完成后将托管在该 WinForm 应用程序中。
为此,我使用 WinForm 元素宿主,并且创建了一个“shell”用户控件,然后创建了该 shell 用户控件内的其他用户控件窗口。因此它显示为一个 WPF 应用程序,并且仅使用 WinForm 应用程序作为其启动项目,因为 WPF 应用程序实际上只是 WPF 控件的集合。
我遇到的问题是,由于我没有创建实际的“WPF 应用程序”,所以没有 App.xaml。这使我无法按照我想要的方式使用共享资源,尤其是 XAML 共享资源。
有没有办法仍然可以将我的 WPF 用户控件集合视为 WPF 应用程序,并以某种方式将 App.xaml 文件用于我的资源。如果没有,我在应用程序中使用共享资源的选项有哪些。
I have run into a little(well big) problem in an application I am working on.
I am working on a module for an application for my company. The application is a WinForm application, but I have been working on a WPF application(not really an application as you will see) that is going to be hosted in this WinForm application when it is complete.
To do this, I am using the WinForm element host, and I have created a "shell" user control, and then other user control windows inside of that shell user control. So it appears as a WPF application, and only uses the WinForm application as its startup project since the WPF application is really only a collection of WPF Controls.
The problem I have ran into is that since I have not created an actual "WPF Application", there is no App.xaml. This has disabled me from using Shared Resources the way I desire, especially XAML shared resources.
Is there a way I can still treat my collection of WPF User Controls as a WPF Application, and somehow use a App.xaml file for my resources. If not, what are my options for using shared resources in my application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
ResourceDictionary
(xaml) 文件添加到您的项目中(假设它是一个类库 - WPF 自定义控件库),将其合并到Generic.xaml
之上,然后您将能够引用它并且您的
StaticResource
将会工作。您还可以将资源包含在 Generic.xaml(或任何 xaml 文件)文件本身中。
您当前的字典应如下所示:
我在 VS2010 中初始化了上述控件的设计时实例,它显示了文本(
Text
是我手动添加到CustomControl1 的字符串 DP 属性)
),这意味着它读取myString
资源。您可以在此处 和
Add a
ResourceDictionary
(xaml) file to your project (assuming it's a class library - WPF custom control library), merge it on top of theGeneric.xaml
, thenyou will be able to refer to it and your
StaticResource
will work.You can also include the resource in the Generic.xaml (or whatever xaml file it is) file itself.
Here is how your current dictionary should look like:
I initialized a design time instance of the above control in VS2010 and it showed the text (
Text
is a string DP property I manually added to theCustomControl1
), which means it reads themyString
resource.You can find some more specific info here and here.