Shared resource dictionary between several user controls and across assemblies

我们两清 提交于 2020-01-12 01:10:43

问题


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?

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

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


回答1:


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

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.

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

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

Update: How to reference resource dictionary stored items from user control.

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.

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:

e.g.

Style="{StaticResource MyButtonStyle}"

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.




回答2:


You can use a Pack URL to reference resource dictionaries across assemblies. For example, in Prism projects (which have multiple modules, each in its own assembly), I store my common resource dictionaries in a ResourceDictionaries folder in a project titled Common. Then I merge these dictionaries as needed into the app's modules by using markup similar to this in each module's XAML view:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Common;component/ResourceDictionaries/DemoDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

You can find further information here.




回答3:


If the "App.xaml" approach doesn't work for you then you might be interessted in this discussion:

Best Practices: How to handle shared resources in a modular application?

jbe



来源:https://stackoverflow.com/questions/703026/shared-resource-dictionary-between-several-user-controls-and-across-assemblies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!