ThemeInfo属性是什么?

Joe*_*ite 30 c# wpf xaml themes assemblyinfo

每当我创建新的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)
)]
Run Code Online (Sandbox Code Playgroud)

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

Ars*_*yan 17

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

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

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

  • 但是,这是什么意思?什么是"主题词典"?什么是"通用词典"?它们适用于什么?它们重要吗?而且,就像我在我的问题中提出的那样,如果删除属性,我会破坏任何东西吗? (5认同)

Dre*_*kes 5

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

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

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

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

您在此处引用的属性是用于映射这些资源的元数据.