将新的 GUID 插入到 Visual Studio 2012 [英] Insert a new GUID to Visual Studio 2012

查看:22
本文介绍了将新的 GUID 插入到 Visual Studio 2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建代码片段或类似的东西来自动化生成 GUID 并将其插入到 Visual Studio 2012 中的文本编辑器的过程?我经常需要生成新的 GUID(例如 WiX 安装程序,以及我们自己的内部框架).

Is it possible to create a code snippet or something similar to automate the process of generating and inserting GUIDs in to the text editor in Visual Studio 2012? I frequently need to generate new GUIDs (WiX installer for example, as well as our own internal framework).

我曾经使用宏来执行这项工作,创建一个新的 GUID,然后将其插入到当前的 ActiveDocument 中.我会将宏绑定到 Shift-Ctrl G,这样我就可以快速插入一个新 ID,而无需启动 Create Guid 工具并从那里复制.

I used to use a macro to perform this job, creating a new GUID and then inserting it in to the current ActiveDocument. I would the bind the macro to Shift-Ctrl G so I could rapidly insert a new ID without having to launch the Create Guid tool and copy from there.

宏功能现已从 Visual Studio 2012 中删除,因此我需要另一种方法,我假设可以使用扩展来实现,但我不熟悉开发扩展,而且这种方法似乎有点笨手笨脚!

Macro functionality has now been removed from Visual Studio 2012 so I need an alternative method, I would assume that it is possible to do with an Extension but I am unfamiliar with developing extensions and that approach would seem a little heavy handed!

任何建议将不胜感激,如果失败,那么将不胜感激有关与文本窗口交互和插入文本所需的扩展类型的任何信息的指针.然后我可以制作一个扩展并将其发布到 Visual Studio Gallery 上.

Any suggestions would be appreciated, failing that then a pointer at any information on what sort of extension would be required to interact with the text window and insert text would be appreciated. I could then make an extension and post it on the Visual Studio Gallery.

谢谢,安东尼

编辑以添加 - 提出的任何解决方案都需要通过键盘快捷键触发".如上所述,我将宏与 Ctrl Shift G 绑定在一起,因为它很容易记住并在编写代码时按下.

Edit to add - whatever solution is proposed would need to be "triggerable" from a keyboard shortcut. As I stated above, I tied the macro to Ctrl Shift G because it was easy to remember and press while writing code.

推荐答案

您可以编写 Visual Studio 2012 扩展来完成此任务!
如果您以前从未编写过加载项,这是一个简单的入门指南!

You could write a Visual Studio 2012 extension to accomplish this!
If you've never written an Add-in before, this is a simple one to get you started!

以下是创建此类加载项的步骤:

Here are the steps to create this type of add-in:

  1. 在 Visual Studio 2012 中创建一个新项目
  2. 选择模板 -> 其他项目类型 -> 可扩展性 -> Visual Studio 插件
  3. 为您的项目命名,然后单击确定".
  4. 按照插件向导步骤操作.出现提示时,选中以下复选框:是的,创建一个‘工具’菜单项."或者,我还检查我的加载项永远不会设置模式 UI..."
  5. 完成向导并在Exec(...)

  1. Create a New Project in Visual Studio 2012
  2. Choose Templates -> Other Project Types -> Extensibility -> Visual Studio Add-in
  3. Name your project, click OK.
  4. Follow the Add-in wizard steps. When prompted, check the box for: "Yes, create a 'Tools' menu item." Optionally, I also check "My Add-in will never put up modal UI..."
  5. Finish the wizard and implement the follow code in Exec(...)

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
    handled = false;
    if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if (commandName == this.GetType().Namespace + ".Connect." + this.GetType().Namespace)
        {
            if (_applicationObject.ActiveDocument != null)
            {
                TextSelection objSel = (EnvDTE.TextSelection)(_applicationObject.ActiveDocument.Selection);

                objSel.Insert(Guid.NewGuid().ToString());
            }

            handled = true;
            return;
        }
    }
}

  • 构建项目,并将AddInName.dllAddInName.AddInAddInName.xml 部署到c:\users\username\documents\Visual Studio 2012\Addins.[如果 Addins 文件夹不存在,请创建它]

  • Build the project, and deploy AddInName.dll, AddInName.AddIn, and AddInName.xml to c:\users\username\documents\Visual Studio 2012\Addins. [If the Addins folder doesn't exist, create it]

    瞧!热键 GUID 生成和一些 Visual Studio 加载项知道如何.:)

    Voila! Hotkey GUID generation and a little bit of Visual Studio Add-in know how. :)

    这篇关于将新的 GUID 插入到 Visual Studio 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆