ThemeInfo 属性有什么用?
每当我创建新的 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)
)]
此 ThemeInfo
属性的用途是什么? 如果我删除它,我会破坏任何东西吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ThemeInfo 属性指定自动主题机制应在何处查找主题字典和通用字典。 每个选项都可以设置为以下值之一:
..dll
,其中
是当前程序集的姓名。
如果主题字典指定外部程序集中定义的控件的样式,例如,
System.Windows.Controls.ProgressBar
和System.Windows.Button
等 WPF 控件,那么您必须使用ThemeDictionaryExtension
将应用程序指定为主题词典的源。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:
<AssemblyName>.<ThemeName>.dll
, where<AssemblyName>
is the current assembly'sname.
If the theme dictionaries specify styles for controls that are defined in external assemblies, for example, the WPF controls such as
System.Windows.Controls.ProgressBar
andSystem.Windows.Button
, then you must use theThemeDictionaryExtension
to specify the application as the source for the theme dictionaries.WPF 框架在控件库中使用此属性作为将资源应用于控件的便捷方法。
考虑到 Windows 可以使用不同的 UI 主题运行(Aero 就是这样的一个例子)。 Microsoft 提供的 WPF 控件会根据不同的环境主题改变其外观。
如果您的应用程序需要这种行为,那么您可以在控件库项目的
themes
文件夹中创建不同的主题字典。即使您不需要多主题支持,也可以方便地将资源放入
generic.xaml
文件中,以便程序集中的控件可以访问它们。 也许您的元素(控件)是在没有.xaml
部分类的.cs
文件中定义的,并且您需要某个地方来存储它所需的资源,或者(更有可能)您拥有将在同一项目/程序集中的许多 WPF 元素之间共享的资源。您此处所指的属性是用于映射这些资源的元数据。
The WPF framework uses this attribute in control libraries as a convenient way to apply resources to controls.
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.
If your application requires this behaviour, then you can create different theme dictionaries the in the
themes
folder of your control library project.Even if you don't need multi-theme support, it is convenient to put resources in the
generic.xaml
file so that they are accessible to controls in the assembly. Perhaps your element (control) is defined in a.cs
file without a.xaml
partial 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.The attribute you're referring to here is metadata for the mapping of these resources.
VS2022 中有一个新的可能性,您可以在这里找到: https: //github.com/dotnet/project-system/issues/3588#issuecomment-1466002423 以及有关 AssemblyAttribute 的更多信息:https://learn.microsoft.com/en-us/dotnet/standard/ assembly/set- attribute-project-file#set-任意-属性
您可以包含
在项目文件中。
请注意,只有当GenerateAssemblyInfo为true(默认或显式设置)时,这才有效。
There is a new possibility in VS2022, which you can find here: https://github.com/dotnet/project-system/issues/3588#issuecomment-1466002423 and some more info about the AssemblyAttribute here: https://learn.microsoft.com/en-us/dotnet/standard/assembly/set-attributes-project-file#set-arbitrary-attributes
You can include
in the project file.
Note that this works only if GenerateAssemblyInfo is true (set by default or explicitly).