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

时间:2022-03-04 13:21:10

I posted a question a few months ago about sharing resource dictionaries across assemblies. It turns out you can do that using the Component Resource Key markup extension. At the time, I could only get it working with a WPF Custom Control project, not with a plain Class Library project.

几个月前我发布了一个关于跨程序集共享资源字典的问题。事实证明,您可以使用Component Resource Key标记扩展来实现。当时,我只能使用WPF自定义控件项目,而不是普通的类库项目。

Now I need to use an existing plain Class Library project to host a shared resource dictionary. That means I need to retrofit the Class Library project to support the Component Resource Key markup extension. I have added a Themes folder and a Generic.xaml resource dictionary document to the Class Library project, as well as references to PresentationCore, PresentationFramework, and WindowsBase. Unfortunately, that doesn't seem to do the trick.

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

So, here is my question: Other than the above, what does a WPF Custom Control Library project have that a plain Class Library project doesn't? Or, to put it another way, what else could I add to my class library project to get this feature working? Thanks.

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

3 个解决方案

#1


6  

Apart from the extra WPF references, the WPF Custom Control Library template has an extra attribute in AssemblyInfo.

除了额外的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 specifies the location in which theme dictionaries are stored for types in an assembly.

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

#2


2  

Another difference is in the .csproj File:

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

In class Library the Tag is missing:

在类库中,标记丢失:

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

After adding it to the first PropertyGroup, the add menu of the project shows now the typical WPF files.

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

#3


1  

Cameron MacFarland's answer was spot on. I have now tested it, and it works.

Cameron MacFarland的回答很明显。我现在测试了它,它的工作原理。

Here is the solution: Add the DLL refs and the Themes/generic.xaml file to the plain Class Library project. Then, open AssemblyInfo.cs and add the following code at the end of the file:

下面是解决方案:将DLL引用和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)
)]

Recompile, and the Component Resource Key markup extension should work.

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

#1


6  

Apart from the extra WPF references, the WPF Custom Control Library template has an extra attribute in AssemblyInfo.

除了额外的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 specifies the location in which theme dictionaries are stored for types in an assembly.

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

#2


2  

Another difference is in the .csproj File:

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

In class Library the Tag is missing:

在类库中,标记丢失:

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

After adding it to the first PropertyGroup, the add menu of the project shows now the typical WPF files.

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

#3


1  

Cameron MacFarland's answer was spot on. I have now tested it, and it works.

Cameron MacFarland的回答很明显。我现在测试了它,它的工作原理。

Here is the solution: Add the DLL refs and the Themes/generic.xaml file to the plain Class Library project. Then, open AssemblyInfo.cs and add the following code at the end of the file:

下面是解决方案:将DLL引用和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)
)]

Recompile, and the Component Resource Key markup extension should work.

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