VS 2012从VSPackage启动UI Windows

时间:2013-10-04 11:42:19

标签: visual-studio-2012 vspackage

我开始使用VS2012可扩展性的可能性。我做了前几次演练,现在我正在努力进一步。我想我正在尝试的很简单......我正在尝试构建一个简单的vspackage,它启动了一个UI窗口。实际上我没有找到任何howto或示例代码。

您是否有关于做类似事情的进一步信息的链接? 谢谢你的帮助......

壹岐

2 个答案:

答案 0 :(得分:1)

我最近一直在寻找解决方案,因为我还需要从VSPackage启动WPF表单。经过几个小时的搜索,我已经有了一些工作,在这个和一些好的试验和错误上搜索各种主题。

我在一个单独的解决方案中有一个现有的WPF-Project,它必须合并到一个VSPackage中。以下是实现此目的的步骤:

  • 创建项目类型'Visual Studio Package'的新解决方案
  • 确保在VS Package中选择“工具窗口”选项 向导(见下图)

VSPackageWizardToolWindowOption

  • 现在已创建解决方案,添加已存在的解决方案 WPF项目(右击“解决方案”,添加 - >现有项目注意:将WPF项目复制到Solution文件夹可能是明智之举在将其添加到解决方案之前。
  • 确保从您的帐户创建对WPF项目的引用 VSPackage-Project和(如有必要)编辑WPF项目的名称空间以满足VSPackage项目的名称空间,或者相反。

您的解决方案现在看起来像这样:

VSPackageSolutionWithWPFProjectAdded

现在,您需要修改 MyToolWindow.cs

// Original:    
base.Content = new MyControl();

// Change to:    
base.Content = new MainWindow();

VSPackage1Package.cs (或调用任何* Package.cs文件)进行以下更改

// Original
        private void ShowToolWindow(object sender, EventArgs e)
        {
            // Get the instance number 0 of this tool window. This window is single instance so this instance
            // is actually the only one.
            // The last flag is set to true so that if the tool window does not exists it will be created.
            ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
            if ((null == window) || (null == window.Frame))
            {
                throw new NotSupportedException(Resources.CanNotCreateWindow);
            }
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }

// Change to:
        private void ShowToolWindow(object sender, EventArgs e)
        {
            // Get the instance number 0 of this tool window. This window is single instance so this instance
            // is actually the only one.
            // The last flag is set to true so that if the tool window does not exists it will be created.
            //ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
            //if ((null == window) || (null == window.Frame))
            //{
            //    throw new NotSupportedException(Resources.CanNotCreateWindow);
            //}
            //IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            //Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
          MainWindow mainwin = new MainWindow();
            mainwin.Show();
        }

如果没有构建错误,那么你应该没问题。

要测试您的WPF表单是否打开,请按“开始”以在新的“实验”Visual Studio实例中运行VSPackage。如果一切正常,您将找到并且应该能够从 View-> Other Windows 菜单中运行WPF。

如果您没有在菜单中看到您的VSPackage,请关闭“实验性”Visual Studio实例。然后清洁en构建您的解决方案并再次按“开始”。它应该现在出现。

答案 1 :(得分:0)

您可以找到初始信息here 这是我的菜单项代码:

    /// <summary>
    /// Initialization of the package; this method is called right after the package is sited, so this is the place
    /// where you can put all the initialization code that rely on services provided by VisualStudio.
    /// </summary>
    protected override void Initialize()
    {
        Debug.WriteLine ("Entering Initialize() of: {0}", this);
        base.Initialize();

        // Add our command handlers for menu (commands must exist in the .vsct file)
        OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
        if ( null != mcs )
        {
            // Create the command for the menu item.
            CommandID menuCommandID = new CommandID(GuidList.guidPackageProject, (int)PkgCmdIDList.Impl);
            OleMenuCommand menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID);
            mcs.AddCommand( menuItem );
        }
    }

    /// <summary>
    /// This function is the callback used to execute a command when the a menu item is clicked.
    /// See the Initialize method to see how the menu item is associated to this function using
    /// the OleMenuCommandService service and the MenuCommand class.
    /// </summary>
    private void MenuItemCallback(object sender, EventArgs e)
    {
        MyForm form = new MyForm();
        form.ShowDialog();   //  Here your form is opening
    }