多个用户控件之间和程序集之间的共享资源字典 [英] Shared resource dictionary between several user controls and across assemblies

查看:40
本文介绍了多个用户控件之间和程序集之间的共享资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个用户控件的程序集.对于这个用户控件程序集,我想要一个资源字典.程序集中的所有用户控件都应该能够访问资源字典.我必须添加吗

I have an assembly that contains several user controls. For this user controls assembly I want to have a resource dictionary. All the user controls within the assembly should be able to access the resource dictionary. Do I have to add

<UserControl.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      ...
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</UserControl.Resources>

对于应该使用资源字典的每个用户控件,或者是否有某种方法可以将它放在一个地方并只引用它?

to every user control that should utilize the resource dictionary, or is there some way of putting it in one place and just referencing it?

然后我可以在我的主应用程序中也引用它,还是需要一个 MergedDictionaries 调用?

can I then also reference it in my main application, or does that require a MergedDictionaries call as well?

主应用程序与用户控件位于不同的项目/程序集中.

the main application is in a separate project/assembly than the user controls.

推荐答案

有什么办法可以把它放在一个地方并引用它吗?

is there some way of putting it in one place and just referencing it?

将合并的字典对资源字典的引用放入App.xaml"文件中,您的整个应用程序都可以访问该文件,您无需进一步引用.

Put your merged dictionaries reference to your resource dictionaries into your 'App.xaml' file and it will be accessible throughout your application, you will need no futher reference.

然后我可以在我的主应用程序中也引用它,还是需要一个 MergedDictionaries 调用?

can I then also reference it in my main application, or does that require a MergedDictionaries call as well?

App.xaml"的范围不属于整个应用程序,所以这应该可以正常工作(对我来说:)).

No the scope of 'App.xaml' falls over the entire application, so this should work fine (does for me :) ).

更新:如何从用户控制中引用资源字典存储的项目.

在主项目中添加对用户控件库的引用.您的用户控件将是可访问的,您可以根据需要在您的应用程序中使用它们.

Within your main project add a reference to your user control library. Your user controls will be accessible and you can use them in you application as desired.

在App.xaml中添加资源字典引用的过程意味着所有控件都可以引用资源字典中定义的样式和数据模板等,因此只需引用它们:

The process of adding the resource dictionary reference in the App.xaml will mean that all controls can reference styles and data templates etc. defined in the resource dictionaries, so it it merely a matter of referencing them:

例如

Style="{StaticResource MyButtonStyle}"

此方法适用于复合应用程序和常规 WPF 应用程序.请注意,Visual Studio 不擅长从链接的 XAML 文件(资源字典)加载这些样式,但表达式混合会处理它并提供编辑器预览.

This method works for both composite applications and regular WPF applications. Note that Visual Studio is no good at loading these styles from Linked XAML files (resource dictionary) but expression blend deals with it and will give the editor preview.

这篇关于多个用户控件之间和程序集之间的共享资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆