我应该在 Visual Studio 加载项中的何处附加解决方案或项目事件?

发布于 2024-09-14 18:53:39 字数 304 浏览 6 评论 0原文

谁能建议将解决方案或项目事件(例如 ProjectAdded)添加到 Visual Studio 加载项的最佳位置?

如果我在加载项连接时执行此操作,则不会加载解决方案,那么如何判断解决方案何时已加载?

例如,如果我编写一个事件来处理添加的项目项,我应该将其附加到哪里?该事件将由项目触发,然后由解决方案触发,因此当外接程序连接时我无法附加事件,因为外接程序连接时没有解决方案。

另一方面,如果我将它们添加到 Exec() 事件中,那么我需要进行检查,例如事件是否已附加,并且我确信连接事件和 Exec 之间有时一定有一种更简洁的方法() 事件。

Can anyone suggest the best place to add solution or project events, such as ProjectAdded, to a Visual Studio add-in?

If I do this when the add-in connects then there's no solution loaded, so how can I tell when a solution has been loaded?

For example, if I write an event to handle project items being added, where should I attach this? The event would be fired by the project, and that in turn by the solution, so I can't attach the events when the add-in connects because there is no solution when the add-in connects.

On the other hand, if I add them in the Exec() event then I need to do checks such as whether the event has been attached already, and I'm sure there must be a neater way sometime between the connection events and the Exec() event.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-09-21 18:53:39

您可能很久以前就明白了这一点,但无论如何:您可以从 OnConnection 中设置事件,如下所示,这是 Addin 的 Connect 类的片段(假设您使用的是 c#)

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;

namespace MyAddin1
{
  /// <summary>The object for implementing an Add-in.</summary>
  /// <seealso class='IDTExtensibility2' />
  public class Connect : IDTExtensibility2, IDTCommandTarget
  {
    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private SolutionEvents _solutionEvents;

    public void OnConnection(object application, ext_ConnectMode connectMode,
          object addInInst, ref Array custom)
    {
      _applicationObject = (DTE2)application;
      _addInInstance = (AddIn)addInInst;

      // check the value of connectMode here, depending on your scenario
      if(connectMode == ...)
        SetupEvents();
    }

    private void SetupEvents()
    {
      // this is important ...
      _solutionEvents = _applicationObject.Events.SolutionEvents;

      // wire up the events you need
      _solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(_solutionEvents_Opened);
      _solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(_solutionEvents_AfterClosing);
      _solutionEvents.ProjectAdded += new _dispSolutionEvents_ProjectAddedEventHandler(_solutionEvents_ProjectAdded);
    }

  // add procedures to handle the events here, plus any other
  // handling you need, ie. OnDisconnection and friends
}

:要点是,要连接所需的解决方案和项目事件,解决方案或项目是否已加载并不重要。它们不附加到任何特定的解决方案或项目,它们由 Visual Studio 对象模型提供并嵌入在 EnvDTE 命名空间。

无论如何,做其他任何事情都没有多大意义,因为您可以配置一个外接程序在 VS 启动时加载,在这种情况下,永远不会加载任何解决方案/项目。

但有一些问题:

  • 重要必须将对 SolutionEvents 类的引用保留为连接类中的成员变量,否则事件永远不会触发,(另请参阅此处)。
  • 您需要确保检查传递给 OnConnectionconnectMode 参数。这会使用不同的参数多次调用,如果您以错误的方式执行此操作,则可能会多次连接该事件,这肯定会是一个问题。此外,通常任何 Addin IDE(如菜单等)都是从 OnConnection 内部设置的,因此如果操作不当,最终可能会出现重复的菜单项。

这里有一些提示,其中提供的一些代码是 VB 代码,以防您正在寻找:

最后,这里是文章列表,其中大约 70% 涵盖有关加载项的基本和高级主题:

找到标题为 MZ-Tools 文章系列(关于加载项) 的部分,以及看看那里涵盖了什么。

You probably figured this out long ago, but anyway: You can setup your events from within OnConnection like shown below, this is a snippet of an Addin's Connect class (assuming you're using c#):

using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;

namespace MyAddin1
{
  /// <summary>The object for implementing an Add-in.</summary>
  /// <seealso class='IDTExtensibility2' />
  public class Connect : IDTExtensibility2, IDTCommandTarget
  {
    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private SolutionEvents _solutionEvents;

    public void OnConnection(object application, ext_ConnectMode connectMode,
          object addInInst, ref Array custom)
    {
      _applicationObject = (DTE2)application;
      _addInInstance = (AddIn)addInInst;

      // check the value of connectMode here, depending on your scenario
      if(connectMode == ...)
        SetupEvents();
    }

    private void SetupEvents()
    {
      // this is important ...
      _solutionEvents = _applicationObject.Events.SolutionEvents;

      // wire up the events you need
      _solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(_solutionEvents_Opened);
      _solutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(_solutionEvents_AfterClosing);
      _solutionEvents.ProjectAdded += new _dispSolutionEvents_ProjectAddedEventHandler(_solutionEvents_ProjectAdded);
    }

  // add procedures to handle the events here, plus any other
  // handling you need, ie. OnDisconnection and friends
}

The main point is, to wire up the solution and project events you need, it's not important if a solution or project is already loaded. They're not attached to any particular solution or project, they're provided by the Visual Studio object model and are embedded within the EnvDTE namespace.

It wouldn't make much sense to do anything else anyway, since you can configure an addin to load when VS starts, and in this case there will never ever be any solutions/projects loaded.

There's a few catches though:

  • It's important that you keep a reference to the SolutionEvents class as a member variable within your connect class, otherwise the events will never fire, (see also here).
  • You need to make sure you check the connectMode parameter passed into OnConnection. This gets called multiple times with different parameters, and if you do it the wrong way you may get the event wired up multiple times, which definetly will be a problem. Also, usually any Addin IDE, like Menus and stuff, is set up from within OnConnection, so you may end up with duplicate menu items if you don't do it right.

Here's a few pointers, some of the code provided is VB code, in case you're looking for that:

Finally, here's a list of articles, about 70% of them cover basic and advanced topics regarding addins:

Find the section entitled MZ-Tools Articles Series (about add-ins) and have a look at what's covered there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文