开发者

WPF/Prism: How to make a resource in my main app trickle down into a usercontrol contained in a separate assembly?

I have the following:

MainApp - Assembly 1 (Contains UserContolA) - Assembly 2 (Contains UserControlB) - Etc.

Not every Assembly will be loaded though, so

Neither assembly can compile if I try to assign resources to controls (button, combobox, etc.) that it can't find开发者_如何学JAVA because I want my MainApp to dictate those resources. But, if I include the resources in the assemblies it seems that I can't override them (because WPF goes from bottom-up for resources giving prioroty to those closest to the bottom).

Also, I'm afraid even if I do solve this problem trying to get it to be dynamic in a way that a user can select from different "themes" that it'd make it even more difficult.

I included prism in the topic because I plan to use that as my framework, but haven't looked deep enough into it to see if it affects this in any way or has something built into it already, which would be a Godsave.


Really what you are asking is what is the best approach to splitting up a WPF application into multiple assemblies. Your strategy is to create reusable control libraries and let the app integrate everything. Great so far. The last issue you need to address is resources. To handle that you need to add a Themes\Generic.xaml and move all the resources in your library into it. Then the library can find all the resources it needs and the application can override it if it so wishes.

Here is a good article that discusses some of the issues related to resources across multiple assemblies:

  • Control Authoring Overview

Also be sure to set up your control assembly attributes so that Generic.xaml will be searched:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None,
    ResourceDictionaryLocation.SourceAssembly)]

Edit:

The above solution applies to custom controls, not to UserControl resources. To handle UserControl resources in a separate assembly that the main application can override, you can use this approach:

  • Use {DynamicResource ...} and simply leave the resource definition out of the UserControl.

The problem with this is that although the resource will be located correctly at run-time, it won't be found during design-time and this can make your UserControl very hard to work with. There are two workarounds for this problem:

  • Use Expression Blend together with its "Design Time Resources" feature. Simply opening a project that has resources that cannot be resolved will cause Blend to prompt you for which resources to use at design-time.
  • Use Visual Studio to design your UserControl and include design time resources in the XAML itself.

For Visual Studio you can include this in your UserControl while designing it only:

<UserControl.Resources>
    <ResourceDictionary Source="/YourControlLibrary;component/DesignTimeResources.xaml"/>
</UserControl.Resources>

Once you are done designing it, simply comment out that code in the XAML. It's inconvenient, but until Visual Studio supports design time resources, at least it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜