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

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

问题描述

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

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

对于VM,我要说的例子是这样的.

int id;
string name;

选择两行,运行一个宏并以

结束

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");
   }
}

我正在寻找有关丢失宏的其他解决方案的想法.

解决方案

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

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

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

创建宏项目

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

现在您有一个外接程序项目.这是您的处理方式:

写代码

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

在课程级别添加此代码:

SolutionEvents solutionEvents;

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

solutionEvents = _applicationObject.Events.SolutionEvents;

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

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

部署

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

  • ShowStartPage.AddIn(来自主项目目录)
  • ShowStartPage.dll(来自项目的构建目录;例如bin \ Debug或bin \ Release)

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

C:\Users\[your user name]\Documents\Visual Studio 2012\Addins

然后退出并重新启动Visual Studio,您应该看到外接程序正在工作.当您转到工具">加载项管理器"时,您也应该看到它列出了.

虽然这比仅打开宏编辑器并将宏代码粘贴在其中更为麻烦,但它的确具有优势,即您可以使用所需的任何语言,而不必陷入那些有点笨拙的VB-就像Visual Studio过去版本中的编辑器一样.

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

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. Create the macro project, stepping through a wizard UI.
  2. Write your code.
  3. Copy the macro's .addin and .dll files to your Visual Studio Addins directory.

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

Create the macro project

  • Run VS 2012 and create a new project.
  • Go to Templates > Other Project Types > Extensibility and select Visual Studio Add-in.
  • Give it a name, such as ShowStartPage.
  • Click OK. This brings up the Add-in Wizard.
  • Step through the wizard, choosing:
    • Programming language: we'll use C#
    • Application host: VS 2012 should be selected
    • Name and description for your add-in
    • On the add-in options page, checkmark only the second option ("I would like my Add-in to load when the host application starts")
    • Skip past the About Box stuff for now, and click Finish.

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

Write the code

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

Add this code at class level:

SolutionEvents solutionEvents;

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

solutionEvents = _applicationObject.Events.SolutionEvents;

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

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.)

Deploy it

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

  • ShowStartPage.AddIn (from your main project directory)
  • ShowStartPage.dll (from your project's build directory; e.g. bin\Debug or bin\Release)

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

C:\Users\[your user name]\Documents\Visual Studio 2012\Addins

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.

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天全站免登陆