ThemeInfo 属性有什么用?

问题描述 投票:0回答:3

每当我创建新的 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
属性有什么用?如果删除它会破坏任何东西吗?

c# wpf xaml themes assemblyinfo
3个回答
20
投票

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

  • 无(默认):不查找资源字典。
  • SourceAssembly:字典是当前程序集。
  • ExternalAssembly:字典位于不同的程序集中,必须命名
    <AssemblyName>.<ThemeName>.dll
    ,其中
    <AssemblyName>
    是当前程序集的 名字。

如果主题字典为外部程序集中定义的控件指定样式,例如

System.Windows.Controls.ProgressBar
System.Windows.Button
等 WPF 控件,则必须使用
ThemeDictionaryExtension
将应用程序指定为主题的源字典。


5
投票

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

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

如果您的应用程序需要此行为,那么您可以在控件库项目的

themes
文件夹中创建不同的主题字典。

即使您不需要多主题支持,也可以方便地将资源放入

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

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


0
投票

VS2022 中有一个新的可能性,您可以在这里找到:https://github.com/dotnet/project-system/issues/3588#issuecomment-1466002423以及有关 AssemblyAttribute 的更多信息:https: //learn.microsoft.com/en-us/dotnet/standard/ assembly/set-attributes-project-file#set-任意-attributes

您可以包括

<ItemGroup>
    <AssemblyAttribute Include="System.Windows.ThemeInfoAttribute">
        <_Parameter1>System.Windows.ResourceDictionaryLocation.None</_Parameter1>
        <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
        <_Parameter2>System.Windows.ResourceDictionaryLocation.SourceAssembly</_Parameter2>
        <_Parameter2_IsLiteral>true</_Parameter2_IsLiteral>
    </AssemblyAttribute>
</ItemGroup>

在项目文件中。
请注意,只有当GenerateAssemblyInfo为true(默认或显式设置)时,这才有效。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.