在我的VSTO应用程序中,我需要使用单独程序集中的资源字典。
我在VSTO应用程序中themes
文件夹下创建了一个名为generic.xaml
的单独资源字典,例如/themes/generic.xaml
。在该文件中,我使用 Pack URI 从外部程序集引用了 ResourceDictionary。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ExternalAssemblyName;component/Themes/resource.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
还将程序集信息更改为以下内容:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]
但还是没有找到运气。任何帮助都会很好。
附言请不要建议将 ResourceDictionary 放在 App.Xaml 中,因为我正在开发 VSTO 应用程序,因此不会有 App.xaml。
使用 VSTO 不会阻止您使用 App.xaml :)
- 像往常一样创建一个 App.xaml,并在 Application.Resources 中导入资源字典
-
在外接程序启动中初始化 WPF 应用程序
private void ThisAddIn_Startup(object sender, System.EventArgs e) { //Ensure the singleton WPF Application is instantiated if (System.Windows.Application.Current == null) { new App(); } //Take control of WPF application shutdown System.Windows.Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown; }
-
确保在关闭外接程序时关闭 WPF 应用程序
private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { if (System.Windows.Application.Current != null) { System.Windows.Application.Current.Shutdown(); } }
-
在 XAML 中使用样式
Github上的示例