ThemeInfo 属性有什么用?

发布于 2024-07-27 20:04:38 字数 599 浏览 4 评论 0 原文

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

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

[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 ThemeInfo attribute for? Will I break anything if I remove it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈下烟灰 2024-08-03 20:04:38

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

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

如果主题字典指定外部程序集中定义的控件的样式,例如,System.Windows.Controls.ProgressBarSystem.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:

  • 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.

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 and System.Windows.Button, then you must use the ThemeDictionaryExtension to specify the application as the source for the theme dictionaries.

注定孤独终老 2024-08-03 20:04:38

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.

哀由 2024-08-03 20:04:38

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-任意-属性

您可以包含

<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(默认或显式设置)时,这才有效。

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

<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>

in the project file.
Note that this works only if GenerateAssemblyInfo is true (set by default or explicitly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文