0

我正在将 VS 插件移植到 VS 包中。包在初始化时订阅 OnBuildBegin 和 OnPublishBegin。Visual Studio 按预期触发 OnBuildBegin,但永远不会调用 OnPublishBegin。

或多或少相同的代码在 Visual Studio 2013、2012 和 2010 中作为插件工作。但是在 VS 2015 作为 VS 包中,OnPublishBegin 似乎不起作用。

示例代码如下。

测试运行配置为在实验模式下启动第二个 VS 实例的调试器的代码。在第二种情况下,我打开一个不同的解决方案并使用发布向导进行发布。

using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace MyPackage
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
    [Guid(VSPackage.PackageGuidString)]
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
    [ProvideAutoLoad(UIContextGuids80.SolutionBuilding)]
    public sealed class VSPackage : Package
    {
        public const string PackageGuidString = "a8ddf848-00ea-4e4e-b11a-65663a8a8021";

        private DTE2 application;
        public VSPackage()
        {
        }

        protected override void Initialize()
        {
            base.Initialize();
            this.application = (DTE2) this.GetService(typeof(DTE));

            ((Events2)this.application.Events).BuildEvents.OnBuildBegin += this.OnBuildBegin;
            ((Events2)this.application.Events).PublishEvents.OnPublishBegin += this.OnPublishBegin;
        }

        private void OnBuildBegin(vsBuildScope scope, vsBuildAction action)
        {
            MessageBox.Show("OnBuildBegin");
        }

        private void OnPublishBegin(ref bool pubContinue)
        {
            MessageBox.Show("OnPublishBegin");
        }
    }
}

任何人都可以为我阐明这个问题吗?

4

1 回答 1

1

强烈建议保留对事件对象的引用以保护它们免受 GC:

protected override void Initialize()
{
    events = application.Events;
    buildEvents = events.BuildEvents;
    publishEvents = events.PublishEvents;
    buildEvents.OnBuildBegin += this.OnBuildBegin;
    publishEvents.OnPublishBegin += this.OnPublishBegin;
}

private Events2 events;
private BuildEvents buildEvents;
private PublishEvents publishEvents;
于 2015-09-16T05:44:31.670 回答