[英]VSTO Add-In CustomTaskPane opens itself after upgrading to Word 2016/2019
我們已經開發了適用於Word 2010的Word VSTO加載項,具有CustomTaskPanes和通過VSTOContrib的 MVVM支持。
升級到Word 2016/2019后,我們的CustomTaskPanes會隨機顯示,而無需用戶采取任何行動。 好像Word注意到何時使用CustomTaskPane並希望下次自動(重新)打開它。
例如,在打開新文檔/退出文檔時,CustomTaskPane打開。 如果它在關閉或保持打開之前不會出現故障(打開,關閉,打開,關閉...),那將不是那么糟糕。 如果CustomTaskPane保持打開狀態,則將無法使用,因為它沒有我們的加載項加載的DataContext。
ThisAddIn中的此代碼創建/刪除CustomTaskPanes:
public CustomTaskPane AddTaskPane(UserControl userControl, string title, Window owner)
{
return CustomTaskPanes.Add(userControl, title, owner);
}
public void RemoveTaskPane(CustomTaskPane taskPane)
{
if (taskPane == null)
return;
CustomTaskPanes.Remove(taskPane);
}
RibbonViewModel(每個文檔/窗口的ViewModel)將這樣調用代碼。 _addInHelper
具有用於創建/刪除CustomTaskPanes以到達ThisAddIn
代碼的事件,並通過回調返回CustomTaskPane實例。 它還使用IoC容器解析視圖"CustomTaskPaneView"
。
// Gets called when a new Window opens or a new Document is opened
public override void Intialize(Document document)
{
// ...
CreateCustomTaskPane();
// ...
}
private void CreateCustomTaskPane()
{
if (_customTaskPane != null)
return;
_addInHelper.AddTaskPane("CustomTaskPaneView", "Custom headline", CurrentWindow, result =>
{
_customTaskPane = result;
});
if (_customTaskPane == null)
{
_log.Error(...);
return;
}
_customTaskPane.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal;
_customTaskPane.Width = Settings.Default.TaskPaneWidth;
_customTaskPane.DockPosition = Settings.Default.TaskPanePosition;
// TaskPane height and width are saved seperately for DockPositionFloating
if (_customTaskPane.DockPosition != MsoCTPDockPosition.msoCTPDockPositionFloating)
{
// Set height and width for DockPositionFloating.
// If the user drags the TaskPane to Floating, it will have the correct size.
var oldDockPosition = _customTaskPane.DockPosition;
_customTaskPane.DockPosition = MsoCTPDockPosition.msoCTPDockPositionFloating;
_customTaskPane.Height = Settings.Default.TaskPaneHeight;
_customTaskPane.Width = Settings.Default.TaskPaneWidth;
_customTaskPane.DockPosition = oldDockPosition;
}
else
{
_customTaskPane.Height = Settings.Default.TaskPaneHeight;
_customTaskPane.Width = Settings.Default.TaskPaneWidth;
}
// Saving/updating settings in these
_customTaskPane.VisibleChanged += ContentControlsTaskPane_OnVisibleChanged;
_customTaskPane.DockPositionChanged += ContentControlsTaskPane_OnDockPositionChanged;
}
關閉窗口/文檔時,此代碼稱為:
public override void Cleanup()
{
if (_customTaskPane != null)
{
SaveCustomTaskPaneProperties();
_contentControlsTaskPane.VisibleChanged -= ContentControlsTaskPane_OnVisibleChanged;
_contentControlsTaskPane.DockPositionChanged -= ContentControlsTaskPane_OnDockPositionChanged;
// Checks if the COM Object was cleaned up already
if (!_contentControlsTaskPane.IsDisposed())
{
// Tried to manually close the CustomTaskPane, but didn't help either
if (_contentControlsTaskPane.Visible)
_contentControlsTaskPane.Visible = false;
// Cleanup the CustomTaskPane ViewModel instance
var taskPaneViewModel = _contentControlsTaskPane.GetViewModel();
taskPaneViewModel?.Dispose();
_addInHelper.RemoveTaskPane(_contentControlsTaskPane);
}
}
}
這僅在使用Word 2016和2019(我們不使用2013)時發生,而在Word 2010中根本沒有發生。 將VSTO項目升級到VSTO加載項2013和2016以進行測試后,情況並沒有得到改善。
我沒有找到任何可能導致此問題的Word選項。 任何想法這可能會導致什么,以及如何解決此問題/解決方法?
編輯
這是更新的代碼示例WordTaskPanesBug
重現步驟:
在示例項目中關閉文檔時,CustomTaskPane也會出現故障,但在我們的實際項目中則不會。
我添加了索引以指示要顯示的任務窗格,該索引表明第二次創建新文檔時要添加的任務窗格來自第一個文檔(第一次創建新文檔時將關閉的任務窗格,可能是因為它是空的)。
我認為您遇到的問題是這個問題: 在VSTO Word加載項中為多個文檔創建和管理自定義任務窗格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.