C# ThemeInfo 属性有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1163013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:42:48  来源:igfitidea点击:

What is the ThemeInfo attribute for?

c#wpfxamlthemesassemblyinfo

提问by Joe White

Whenever I create a new WPF application or WPF user control library, the AssemblyInfo.csfile includes the following attribute:

每当我创建新的 WPF 应用程序或 WPF 用户控件库时,该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)
)]

What is this ThemeInfoattribute for? Will I break anything if I remove it?

这个ThemeInfo属性有什么用?如果我删除它,我会破坏任何东西吗?

回答by Arsen Mkrtchyan

ThemeInfo attribute specifies where the automatic theming mechanism should look for the theme dictionaries and the generic dictionary. Each option can be set to one of the following values:

ThemeInfo 属性指定自动主题化机制应在何处查找主题词典和通用词典。每个选项都可以设置为以下值之一:

  • None (default): Don't look for a resource dictionary.
  • SourceAssembly: The dictionary is the current assembly.
  • ExternalAssembly: The dictionary is in a different assembly, which must be named <AssemblyName>.<ThemeName>.dll, where <AssemblyName>is the current assembly's name.
  • 无(默认):不查找资源字典。
  • SourceAssembly:字典是当前程序集。
  • ExternalAssembly:字典在不同的程序集中,必须命名为 <AssemblyName>.<ThemeName>.dll,其中<AssemblyName>是当前程序集的名称。

If the theme dictionaries specify styles for controls that are defined in external assemblies, for example, the WPF controls such as System.Windows.Controls.ProgressBarand System.Windows.Button, then you must use the ThemeDictionaryExtensionto specify the application as the source for the theme dictionaries.

如果主题词典为在外部程序集中定义的控件指定样式,例如 WPF 控件System.Windows.Controls.ProgressBarSystem.Windows.Button,则必须使用ThemeDictionaryExtension将应用程序指定为主题词典的源。

回答by Drew Noakes

The WPF framework uses this attribute in control libraries as a convenient way to apply resources to controls.

WPF 框架在控件库中使用此属性作为将资源应用到控件的便捷方式。

Consider that Windows can be run with different UI themes (Aero is one such example). The WPF controls provided by Microsoft alter their appearance for different environment themes.

考虑到 Windows 可以使用不同的 UI 主题运行(Aero 就是这样的一个例子)。Microsoft 提供的 WPF 控件会针对不同的环境主题更改其外观。

If your application requires this behaviour, then you can create different theme dictionaries the in the themesfolder of your control library project.

如果您的应用程序需要此行为,那么您可以在themes控件库项目的文件夹中创建不同的主题词典。

Even if you don't need multi-theme support, it is convenient to put resources in the generic.xamlfile so that they are accessible to controls in the assembly. Perhaps your element (control) is defined in a .csfile without a .xamlpartial class, and you need somewhere to store the resources it needs, or (more likely) you have resources that will be shared between many WPF elements in the same project/assembly.

即使您不需要多主题支持,将资源放入generic.xaml文件中也很方便,以便程序集中的控件可以访问它们。也许您的元素(控件)是在.cs没有.xaml分部类的文件中定义的,并且您需要某个地方来存储它需要的资源,或者(更有可能)您拥有将在同一项目/程序集中的许多 WPF 元素之间共享的资源。

The attribute you're referring to here is metadata for the mapping of these resources.

您在此处所指的属性是用于映射这些资源的元数据。