当与WPF窗口一起使用时,MS excel加载项将停止显示在excel功能区中



我创建了一个WPF窗口(非模态(,点击MS excel加载项功能区中的按钮即可打开。我遇到了WPF窗口中不可编辑的文本框的问题,我试图使用上给出的解决方案来克服这个问题:来自MS Excel插件的WPF无模式对话框

现在,WPF窗口中文本框不可编辑的问题得到了解决。但是,当我关闭excel文件并再次打开它时,MS excel加载项选项卡停止显示(此外,当我打开excel文件时,我可以在任务管理器的后台流程下看到excel流程(。因此,每次打开excel文件时,我都需要手动删除并重新添加excel加载项。这对于用户环境来说真的是不可接受的。请建议是否有解决方案来克服这一问题。

这是我在按钮(出现在外接程序功能区上(单击事件处理程序上编写的代码。

Thread newWindowThread = new Thread(new ThreadStart(() =>
{
// Create our context, and install it:
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(
Dispatcher.CurrentDispatcher));

Window1 tempWindow = new Window1(ExcelApp.ActiveWorkbook.Name);
// When the window closes, shut down the dispatcher
tempWindow.Closed += (s, e) =>
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
tempWindow.Show();
// Start the Dispatcher Processing
System.Windows.Threading.Dispatcher.Run();
}));
// Set the apartment state
newWindowThread.SetApartmentState(ApartmentState.STA);
// Make the thread a background thread
//newWindowThread.IsBackground = true;
// Start the thread
newWindowThread.Start(); 

有两种方法可以解决这个问题。1( 不要使用螺纹。相反,请执行以下操作:-

Window1 tempWindow=新Window1(ExcelApp.ActiveWorkbook.Name(;tempWindow.ShowDialog((

但由于ShowDialog((方法,它阻止了Excel。

2( 按照下面的链接使用Excel Dna在Excel中使用Wpfhttps://github.com/AddinX/Sample.Wpf/tree/master/src

最新更新