本文介绍了什么是订阅EnvDTE80.DTE2.Events2.PublishEvents.OnPublishBegin正确的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植的VS插件到VS包。包订阅包时被初始化为OnBuildBegin和OnPublishBegin。 Visual Studio的触发OnBuildBegin如预期,但OnPublishBegin永远不会被调用。



或多或少相同的代码工作在Visual Studio 2013,2012,和2010年作为一个插件。但在2015年VS作为VS包,OnPublishBegin似乎没有发挥作用。



示例代码如下。



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



使用系统.Diagnostics.CodeAnalysis;使用System.Runtime.InteropServices
;使用System.Windows.Forms的
;
使用EnvDTE;
使用EnvDTE80;
使用Microsoft.VisualStudio.Shell;
使用Microsoft.VisualStudio.Shell.Interop;

命名空间MyPackage的
{
[PackageRegistration(UseManagedResourcesOnly =真)]
[InstalledProductRegistration(#110,#112,1.0,IconResourceID = 400)] //信息关于这个软件包的帮助/关于
[的Guid(VSPackage.PackageGuidString)
[SuppressMessage(StyleCop.CSharp.DocumentationRules,SA1650:ElementDocumentationMustBeSpelledCorrectly,理由=pkgdef ,VS和vsixmanifest是VS条款有效)]
[ProvideAutoLoad(UIContextGuids80.SolutionBuilding)
公共密封类VSPackage的:包
{
公共常量字符串PackageGuidString =a8ddf848- 00ea-4e4e-B11A-65663a8a8021

私人DTE2申请;
公共VSPackage的()
{
}

保护覆盖无效初始化()
{
base.Initialize();
this.application =(DTE 2)this.GetService(typeof运算(DTE));

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

私人无效OnBuildBegin(vsBuildScope范围,vsBuildAction动作)
{
MessageBox.Show(OnBuildBegin);
}

私人无效OnPublishBegin(参考布尔pubContinue)
{
MessageBox.Show(OnPublishBegin);
}
}
}



任何人都可以阐明光?对我来说问题


解决方案

强烈建议保持引用事件的对象,以保护他们免受GC:

 保护覆盖无效初始化()
{
事件= application.Events;
buildEvents = events.BuildEvents;
publishEvents = events.PublishEvents;
buildEvents.OnBuildBegin + = this.OnBuildBegin;
publishEvents.OnPublishBegin + = this.OnPublishBegin;
}

私人Events2活动;
私人BuildEvents buildEvents;
私人PublishEvents publishEvents;


I'm porting an VS addin to a VS Package. The package subscribes to OnBuildBegin and OnPublishBegin when the package is initialized. Visual Studio triggers OnBuildBegin as expected, but OnPublishBegin is never called.

More or less the same code work in Visual Studio 2013, 2012, and 2010 as an addin. But in VS 2015 as a VS Package, OnPublishBegin doesn't appear to be functional.

Sample Code is below.

To test the code running the debugger configured to start a second instance of VS in Experiment Mode. In the second instance, I open a different solution and publish using the Publish Wizard.

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

Can anyone shed light on the problem for me?

解决方案

It is highly recommended to keep references to Events objects to protect them from 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;

这篇关于什么是订阅EnvDTE80.DTE2.Events2.PublishEvents.OnPublishBegin正确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 09:16