mYn*_*EAm 2 c# wpf vspackage visual-studio visual-studio-2013
所以我的visual studio扩展(包)中有两个工具窗口,我想通过第一个窗口上的按钮打开第二个窗口.
我希望在这里解释一下:"如何:以编程方式打开工具窗口",但事实并非如此.
您应该使用Package.FindToolWindow或IVsUIShell.FindToolWindow查找或创建工具窗口.
如果从你自己的包中使用(或者你有一个对包的引用,只需将它放在那里而不是这个):
private void OpenFromPackage()
{
ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true); // True means: crate if not found. 0 means there is only 1 instance of this tool window
if (null == window || null == window.Frame)
throw new NotSupportedException("MyToolWindow not found");
IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
Run Code Online (Sandbox Code Playgroud)
如果您无法从包中执行此操作,或者没有对其进行引用,请使用IVSUIShell:
private void OpenWithIVsUIShell()
{
IVsUIShell vsUIShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
Guid guid = typeof(MyToolWindow).GUID;
IVsWindowFrame windowFrame;
int result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFindFirst, ref guid, out windowFrame); // Find MyToolWindow
if (result != VSConstants.S_OK)
result = vsUIShell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fForceCreate, ref guid, out windowFrame); // Crate MyToolWindow if not found
if (result == VSConstants.S_OK) // Show MyToolWindow
ErrorHandler.ThrowOnFailure(windowFrame.Show());
}
Run Code Online (Sandbox Code Playgroud)