0

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

你有一些关于做类似事情的更多信息的链接吗?谢谢你的帮助。。

壹岐

4

2 回答 2

0

您可以在此处找到初始信息。
这是我的菜单项代码:

    /// <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
    }
于 2013-10-08T08:18:35.750 回答
0

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

我在一个单独的解决方案中有一个现有的 WPF 项目,它必须合并到一个 VSPackage 中。以下是使其工作的步骤:

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

VSPackageWizardToolWindowOption

  • 既然已经创建了解决方案,请将已经存在的 WPF 项目添加到其中(右键单击“解决方案”,添加->现有项目注意:在添加之前将 WPF 项目复制到解决方案文件夹可能是明智之举它到解决方案。
  • 确保从 VSPackage-Project 创建对 WPF-Project 的引用,并(如有必要)编辑 WPF-Project 的命名空间以满足 VSPackage-Project 的命名空间,或者反过来。

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

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 实例。然后 Clean en Build your Solution 并再次按“开始”。它现在应该出现。

于 2016-06-08T12:12:54.503 回答