WPF modal window in Visual Studio Extension blocki

2019-03-03 02:26发布

问题:

Using the following tutorial, within a VSIX project, I created a WPF window that inherits from Microsoft.VisualStudio.PlatformUI.DialogWindow, and I display this modally using the following code:

var myWindow = new MyWindow(myParameters);
myWindow.ShowDialog();

When I compile this in RELEASE mode and start without debugging [Ctrl+F5], it opens the Experimental version of Visual Studio. Here, I open another Solution and then execute my Modal Window. The window works just fine - I can type in text boxes etc, etc.

However, when I close the modal dialog window [using this.Close()], the problems start. If I navigate to one of the documents in the open solution, I can type, but the keyboard buttons backspace [<-] and [Delete] (and possibly others too) are simply ignored....I can't delete what I've just typed!

Also, when I try to close this experimental version of Visual Studio, I get the following message:

Microsoft Visual Studio has detected that an operation is blocking user input. This can be caused by an active modal dialog or a task that needs to block user interaction. Would you like to shut down anyway?

But, as far as I can tell, my modal window has been closed and may even have been garbage collected by the time I get to close this instance of Visual Studio.

This isn't limited to the Experimental version - when I push this VSIX to our local gallery and install as an Extension, then I get the same behaviour.

I have also tried explicitly setting the owner, but his had no effect on this problem:

var myWindow = new MyWindow(myParameters)
{
    Owner = Application.Curent.MainWindow
}
myWindow.ShowDialog();

If I make this a non-modal window, then I get different (but related) problems. Here, if I open the Experimental version of Visual Studio and open the other Solution where I navigate to a C# page. I then open my Extension's WPF window where I can happily type into TextBoxes in that WPF window. However, whenever I click the backspace [<-] or the [delete] keys, this doesn't affect the current WPF textbox but the previously opened C# code window in the current solution. See a previous post on this

What am I missing?

回答1:

The following suggestion found here seems to work for me:

IVsUIShell uiShell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell));

uiShell.EnableModeless(0);

var myWindow = new MyWindow(myParameters)
{
    Owner = Application.Curent.MainWindow
}
myWindow.ShowDialog();

uiShell.EnableModeless(1);