We've developed a Word VSTO Add-In for Word 2010 with CustomTaskPanes and MVVM Support through VSTOContrib.
After upgrading to Word 2016/2019 our CustomTaskPanes show up randomly without any action from the user.
It seems like that Word notices when a CustomTaskPane was used and wants to (re)open it automatically the next time.
For example, a CustomTaskPane opens while opening a new/exisitng document. Wouldn't be that bad, if it wouldn't glitch (open, close, open, close, ...) till it closes or stays open. If the CustomTaskPane stays open, it is unusable because it has no DataContext that was loaded by our Add-In.
This Code in ThisAddIn creates/removes 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);
}
The RibbonViewModel (ViewModel per Document/Window) calls the Code like this.
The _addInHelper
has events for creating/removing CustomTaskPanes to reach the ThisAddIn
Code and returns the CustomTaskPane instance by callback. It also uses the IoC Container to resolve the view "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;
}
When closing the Window/Document, this code is called:
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);
}
}
}
This only happens while using Word 2016 and 2019 (we don't use 2013) and didn't happen with Word 2010 at all. After upgrading the VSTO Project to VSTO Add-In 2013 and 2016 for testing purposes, it doesn't get better.
I didn't find any Word options that could cause this. Any idea what this could cause and how to fix this / getting a workaround?
EDIT
Here is the updated code example WordTaskPanesBug
Steps to reproduce:
- Start Word / run project
- Click "Open" button
- Click "New document" button
- Click "New document" button, TaskPane gets opened (but won't glitch this time)
Also the CustomTaskPane glitches while closing the document in the example project, but not in our real project.
test.docx
resource to be able to quickly open the same document. Maybe you could have a look again? Thanks in advice. – julianstark999