用途:在 WPF 自定义控件库项目中配置 ThemeInfo 特性,让运行时自动定位并加载主题资源字典,使 CustomControl 样式生效。缺少该特性时,Themes/Generic.xaml 不会被自动加载,控件样式不会应用。
主要能力:在 Properties/AssemblyInfo.cs 中声明程序集级 ThemeInfo,指定主题专用资源与通用资源的位置。通用资源需设为 SourceAssembly,以从本程序集的 Themes/Generic.xaml 加载;主题专用资源常用 None。可选值包括 None、SourceAssembly、ExternalAssembly(主题打包在独立程序集时使用)。
必要用法:在 AssemblyInfo.cs 添加 using System.Windows; 并写入 [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]。文件须位于 Properties/AssemblyInfo.cs,Generic.xaml 须位于 Themes/Generic.xaml。每个 CustomControl 还需在静态构造函数中通过 DefaultStyleKeyProperty.OverrideMetadata 设置默认样式键,与 ThemeInfo 配合定位样式。
限制与注意:不要用 App.xaml 的 MergedDictionaries 加载 CustomControl 样式,这不符合标准模式,跨程序集引用时会失效并破坏封装。样式不生效时依次检查 ThemeInfo 是否存在、第二个参数是否为 SourceAssembly、是否设置 DefaultStyleKeyProperty、Generic.xaml 路径是否正确。