您的位置:首页 > 编程语言 > C#

Visual Studio 2015 插件开发 从入门到放弃(一)

2016-11-20 17:37 573 查看


古银有了个人博客了:小古银的官方网站

必需知识:C#和Visual Studio

(1)安装 Visual Studio 2015 的时候选择 Visual Studio 扩展性工具Visual Studio Extensibility Tools)。对于已经安装好 Visual Studio 2015 的小伙伴,可以找VS2015的安装包,或者在程序与功能中找VS2015双击,在VS2015界面中选择修改,再选择Visual Studio 扩展性工具



(2)安装完成后打开 VS2015,创建一个名为 FirstMenuCommand 的 VSIX Project:文件(File)->新建(New)->项目(Project)->Visual C#->Extensibility->VSIX Project,名称改为 FirstMenuCommand





(3)工程打开后,添加一个名为 FirstCommand 的 custom command 模板:在 解决方案资源管理器(Solution Explorer)中,右键工程节点,选择添加(Add)->新建项(New Item)->Visual C# 项->Exitensibility->Custom Command,在 名称 中填入FirstCommand.cs





(4)编译工程并且运行调试,将会出现一个调试用的 Visual Studio 出现。

(5)在用于调试的 Visual Studio 中,点击 工具(Tools)->扩展和更新(Extensions and Updates),在打开的窗口中就会找到FirstMenuCommand 插件。





(6)关闭扩展和更新窗口,在 工具(Tools)中会看到 Invoke FirstCommand 菜单命令。点击将会出现消息框,显示“FirstCommandPackage Inside FirstMenuCommand.FirstCommand.MenuItemCallback()”。





(7)停止调试。在原来的VS上打开 FirstCommand.cs 文件。添加  using 语句:

using System.Diagnostics;
(8)找到 FirstCommand 的私有构造方法,改成如下:

private FirstCommand(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}

this.package = package;

OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);

// 修改成StartNotepad句柄
MenuCommand menuItem = new MenuCommand(this.StartNotepad, menuCommandID);
commandService.AddCommand(menuItem);
}
}
(9)添加方法 StartNotepad :

private void StartNotepad(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.Start();
}

(10)开始运行调试,点击 工具->Invoke FirstCommand ,将会弹出记事本程序。

FirstCommand.cs的全部代码:

using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Diagnostics; // 新增using语句

namespace FirstMenuCommand
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class FirstCommand
{
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 0x0100;

/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("8bacb01b-5991-43cc-9acf-538bab2ca16e");

/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly Package package;

/// <summary>
/// Initializes a new instance of the <see cref="FirstCommand"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private FirstCommand(Package package) { if (package == null) { throw new ArgumentNullException("package"); } this.package = package; OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (commandService != null) { var menuCommandID = new CommandID(CommandSet, CommandId); // 修改成StartNotepad句柄 MenuCommand menuItem = new MenuCommand(this.StartNotepad, menuCommandID); commandService.AddCommand(menuItem); } }

/// <summary>
/// Gets the instance of the command.
/// </summary>
public static FirstCommand Instance
{
get;
private set;
}

/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}

/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>

4000
/// <param name="package">Owner package, not null.</param>
public static void Initialize(Package package)
{
Instance = new FirstCommand(package);
}

/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void MenuItemCallback(object sender, EventArgs e)
{
string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
string title = "FirstCommand";

// Show a message box to prove we were here
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
message,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}

// 新增StartNotepad方法用来打开记事本
private void StartNotepad(object sender, EventArgs e) { Process process = new Process(); process.StartInfo.FileName = "notepad.exe"; process.Start(); }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息