WPF自定义控件库和普通类库之间的区别?

时间:2010-01-18 20:39:22

标签: wpf prism

几个月前我发布了一个关于sharing resource dictionaries across assemblies的问题。事实证明,您可以使用Component Resource Key标记扩展来实现。当时,我只能使用WPF自定义控件项目,而不是普通的类库项目。

现在我需要使用现有的普通类库项目来托管共享资源字典。这意味着我需要改进类库项目以支持组件资源键标记扩展。我已将Themes文件夹和Generic.xaml资源字典文档添加到类库项目,以及对PresentationCore,PresentationFramework和WindowsBase的引用。不幸的是,这似乎没有成功。

所以,这是我的问题:除了上述内容之外,WPF自定义控件库项目对普通类库项目没有什么影响?或者,换句话说,我还可以添加到我的类库项目中以使此功能正常工作吗?感谢。

3 个答案:

答案 0 :(得分:6)

除了额外的WPF引用之外,WPF自定义控件库模板在AssemblyInfo中还有一个额外的属性。

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

ThemeInfoAttribute指定为程序集中的类型存储主题词典的位置。

答案 1 :(得分:2)

另一个区别在于.csproj文件:

在类库中缺少标记:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

将其添加到第一个PropertyGroup后,项目的添加菜单现在显示典型的WPF文件。

答案 2 :(得分:1)

卡梅伦麦克法兰的回答很明显。我现在测试了它,它的工作原理。

以下是解决方案:将DLL refs和Themes / generic.xaml文件添加到普通的类库项目中。然后,打开AssemblyInfo.cs并在文件末尾添加以下代码:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

重新编译,组件资源键标记扩展应该有效。