替代 Visual Studio 2012 中的宏 [英] Alternative to macros in Visual Studio 2012

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

问题描述

我在 XAML 开发中将宏广泛用于 ViewModel 属性.我在 WCF 中更多地使用它们来生成 Message 和 DataContract 属性.

I use macros extensively for ViewModel properties in XAML development. I use them even more in WCF to generate Message and DataContract properties.

令我失望的是,我构建的宏将无法在 Visual Studio 2012 中使用.

To my disappointment, the macros I've built aren't going to be usable in Visual Studio 2012.

我正在谈论的一个例子,对于虚拟机,我会输入这样的内容.

An example of what I'm talking about, for a VM, I would enter something like this.

int id;
string name;

选择两行,运行宏并结束

Select both lines, run a macro and end up with

private int _id;
private string _name;

public int Id
{
   get {return _id;}
   set
   {
      if(_id != value)
      {
        _id = value;
        RaisePropertyChanged("Id");
      }
}

public string Name
{
   if(_name != value)
   {
      _name = value;
      RaisePropertyChanged("Name");
   }
}

我正在寻找其他解决宏丢失问题的想法.

I'm looking for ideas of other solutions deal with losing macros.

推荐答案

宏的最简单替代方法是创建加载项.我知道,我知道,我也不对此感到兴奋,但它实际上出奇的简单.它包含三个简单的部分:

The simplest alternative to macros is creating add-ins. I know, I know, I wasn't excited about it either, but it's actually surprisingly easy. There are three simple parts to it:

  1. 创建宏项目,逐步完成向导 UI.
  2. 编写代码.
  3. 将宏的 .addin 和 .dll 文件复制到 Visual Studio Addins 目录.

让我们看看 我写的一个简单的宏,用于在关闭解决方案后显示起始页 并将其转换为加载项.

Let's take a simple macro I wrote to show the Start Page after closing a solution and turn it into an add-in.

  • 运行 VS 2012 并创建一个新项目.
  • 转到模板 > 其他项目类型 > 可扩展性,然后选择 Visual Studio 插件.
  • 为其命名,例如 ShowStartPage.
  • 单击确定".这会打开插件向导.
  • 逐步完成向导,选择:
    • 编程语言:我们将使用 C#
    • 应用程序主机:应选择 VS 2012
    • 加载项的名称和说明
    • 在加载项选项页面上,仅选中第二个选项(我希望我的加载项在宿主应用程序启动时加载")
    • 暂时跳过关于框的内容,然后点击完成.

    现在您有了一个插件项目.您可以这样做:

    Now you have an add-in project. Here's what you do with it:

    打开 Connect.cs 文件.(它可能已经打开了.一些DTE"的东西应该看起来很熟悉.)

    Open the Connect.cs file. (It might already be open. Some of the "DTE" stuff should look familiar.)

    在类级别添加此代码:

    SolutionEvents solutionEvents;
    

    将此代码添加到 OnConnection 方法中,紧跟在 _addInInstance = (AddIn)addInInst; 行之后:

    Add this code to the OnConnection method, right after the _addInInstance = (AddIn)addInInst; line:

    solutionEvents = _applicationObject.Events.SolutionEvents;
    
    solutionEvents.AfterClosing += () =>
    {
        _applicationObject.ExecuteCommand("View.StartPage");
    };
    

    点击运行"按钮来测试您的代码.一个新的 Visual Studio 2012 实例启动,加载了你的加载项.现在测试加载项并确保它正常工作.(打开一个解决方案,然后关闭它;当你这样做时,起始页应该返回.)

    Hit the "Run" button to test your code. A new instance of Visual Studio 2012 starts up, with your add-in loaded. Now test the add-in and make sure it works. (Open a solution, then close it; the Start Page should return when you do.)

    加载项工作后,要在 Visual Studio 2012 中定期使用它,您只需部署两个文件:

    Once the add-in works, to use it regularly with Visual Studio 2012, you only need to deploy two files:

    • ShowStartPage.AddIn(来自您的主项目目录)
    • ShowStartPage.dll(来自项目的构建目录;例如 binDebug 或 binRelease)
    • ShowStartPage.AddIn (from your main project directory)
    • ShowStartPage.dll (from your project's build directory; e.g. binDebug or binRelease)

    将这两个文件放在 VS 2012 加载项目录中,可能位于此处:

    Put those two files in your VS 2012 add-ins directory, probably located here:

    C:Users[您的用户名]DocumentsVisual Studio 2012Addins

    然后退出并重新启动 Visual Studio,您应该会看到您的加载项正在工作.当您转到工具">插件管理器"时,您还应该看到它列出来.

    Then exit and restart Visual Studio, and you should see your add-in working. You should also see it listed when you go to Tools > Add-in Manager.

    虽然这比打开宏编辑器并将宏代码粘贴在其中更麻烦,但它的优点是你可以使用任何你想要的语言,而不是被困在有点不稳定的 VB-就像过去版本的 Visual Studio 中的编辑器一样.

    While this is a bit more of a nuisance than just opening the macro editor and sticking your macro code in there, it does have the advantage that you can use any language you want, instead of being stuck with the somewhat flaky VB-like editor in past versions of Visual Studio.

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

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