我正在编写一个 VS 插件,我需要在成功构建后运行某种方法。我尝试过使用 dte.Events.BuildEvents.OnBuildDone 但即使构建失败该事件也会发生。

是否有我应该使用的财产或其他事件?

有帮助吗?

解决方案

在OnBuildDone事件不能告诉你发生了什么。在该解决方案的一些项目可能已经正确建立,有的没有。你需要OnBuildProjConfigDone代替。每个项目的火灾,成功参数告诉你,如果它的工作。

其他提示

通常情况下,你需要处理正在兴建多个项目。这可能是一个解决方案生成,或者建设项目是依赖于另一个项目。

所以,弄清楚当一个成功的构建完成后,您需要使用两个构建事件的组合:

和OnBuildProjConfigDone OnBuildDone。

您还需要一个成员变量来跟踪整个构建状态。

您OnBuildProjConfigDone处理程序将调用该被建成每一个项目,它被传递一个布尔告诉你,项目建设是否成功。这个结果分配给你的成员变量来跟踪的整体状态。

最后,您OnBuildDone处理程序将被调用。在这里,你可以看看你的成员变量,看是否有项目生成失败。

下面是从一个扩展我写VS2012一些示例代码。的扩展提供了构建活动项目和发射调试器如果构建是成功的一个“自定义生成”命令。

private bool _overallBuildSuccess;
private bool _customBuildInProgress;

private void CustomBuild_MenuItemCallback(object sender, EventArgs e)
{
    // Listen to the necessary build events.
    DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
    dte.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
    dte.Events.BuildEvents.OnBuildProjConfigDone += BuildEvents_OnBuildProjConfigDone;

    try
    {
        // Build the active project.
        _customBuildInProgress = true;
        dte.ExecuteCommand("Build.BuildSelection");
    }
    catch (COMException)
    {
        _customBuildInProgress = false;
        WriteToOutputWindow("Build", "Could not determine project to build from selection");
    }
}

private void BuildEvents_OnBuildProjConfigDone(string project, string projectConfig, string platform, string solutionConfig, bool success)
{
    // Ignore this build event if we didn't start it.
    if (!_customBuildInProgress)
    {
        return;
    }

    // Keep track of the overall build success.
    _overallBuildSuccess = success;
}

private void BuildEvents_OnBuildDone(EnvDTE.vsBuildScope scope, EnvDTE.vsBuildAction action)
{
    // Ignore this build event if we didn't start it.
    if (!_customBuildInProgress)
    {
        return;
    }

    _customBuildInProgress = false;

    if (_overallBuildSuccess)
    {
        // Launch the debugger.
        DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
        dte.ExecuteCommand("Debug.Start");
    }
    else
    {
        WriteToOutputWindow("Build", "Custom build failed.");
    }
}

private void WriteToOutputWindow(string paneName, string message)
{
    DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));

    Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
    OutputWindow outputWindow = (OutputWindow)window.Object;

    OutputWindowPane targetPane = outputWindow.OutputWindowPanes.Cast<OutputWindowPane>()
        .FirstOrDefault(x => x.Name.ToLower() == paneName.ToLower());

    if (targetPane == null)
    {
        targetPane = outputWindow.OutputWindowPanes.Add(paneName);
    }

    targetPane.Activate();
    outputWindow.ActivePane.OutputString(message);
    outputWindow.ActivePane.OutputString(Environment.NewLine);
}

对于未来的读者,请查看这篇文章。

http://blogs.msdn.com/b/alexpetr/archive/2012/08/14/visual-studio-2012-and-buildevents-in-addins.aspx

和/或

http://support.microsoft.com/kb/555102/en-us

基本上,可能存在错误。解决方法是在 Connect 上设置“.BuildEvents”的成员变量。

例子:

private _BuildEvents _buildEvents;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
              {
                _buildEvents = _applicationObject.Events.BuildEvents;
              }

然后将事件处理程序连接到

this._buildEvents

并不是

_applicationObject.Events.BuildEvents

其中 _applicationObject = (EnvDTE.DTE)application;

至少值得一试,恕我直言。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top