如何:响应 ClickOnce 发布事件

使用 ClickOnce 可以将 Windows 应用程序发布到 Web 服务器或进行网络文件共享以进行简化安装。 有关更多信息,请参见 ClickOnce 安全和部署

Visual Studio 核心自动化模型(包含在 EnvDTE80 中)具有一个名为 PublishEvents 的事件处理对象。 它可以用于检测 ClickOnce 部署开始 (OnPublishBegin) 和结束 (OnPublishDone) 的时间。 您可以根据这些事件执行操作。 下面的过程演示如何处理这些事件。

提示

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。 这些过程是在“常规开发设置”处于活动状态时开发的。 若要更改设置,请在“工具”菜单上选择“导入和导出设置”。 有关更多信息,请参见 使用设置

响应 ClickOnce 事件

  1. 在新建的或现有的外接程序中,将下列代码添加到 Connect 类中。

  2. 按照如何:使用外接程序管理器控制外接程序中所述生成和激活外接程序。

  3. 按照 ClickOnce 安全和部署中所述开始 ClickOnce 部署操作。

    在部署开始和结束时会出现一个消息框。

示例

Public Class Connect
    Implements IDTExtensibility2
    Public WithEvents pubEvents As EnvDTE80.PublishEvents

    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn

    Public Sub OnConnection(ByVal application As Object, ByVal _
    connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
    custom As Array) 
    Implements IDTExtensibility2.OnConnection
        Try
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        Try
            _applicationObject = CType(application, DTE2)
            _addInInstance = CType(addInInst, AddIn)
            Dim events As EnvDTE80.Events2
            events = CType(_applicationObject.Events2, Events2)
            pubEvents = CType(events._PublishEvents(Nothing), _
            EnvDTE80.PublishEvents)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub pubEvents_OnPublishBegin(ByRef [Continue] As Boolean) _
    Handles pubEvents.OnPublishBegin
        MsgBox("A publish event is occuring…")
    End Sub

    Private Sub pubEvents_OnPublishDone(ByVal Success As Boolean) _
    Handles pubEvents.OnPublishDone
        MsgBox("A publish event has completed.")
    End Sub

    Public Sub OnDisconnection(ByVal disconnectMode As _
      ext_DisconnectMode, ByRef custom As Array) Implements _
      IDTExtensibility2.OnDisconnection
        pubEvents = Nothing
    End Sub
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
namespace MyAddin2
{
    public class Connect : IDTExtensibility2
    {
        public Connect()
        {
        }

        public void OnConnection(object application,   
           ext_ConnectMode connectMode, object addInInst, ref Array 
           custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            try
            {
                _applicationObject = (DTE2)application;
                _addInInstance = (AddIn)addInInst;
                EnvDTE80.Events2 events;
                events = (Events2)_applicationObject.Events;
                // Retrieve the delegates for the Publish events.
                pubEvents = (EnvDTE80.PublishEvents)
                  events.PublishEvents;
                // Connect to the delegates exposed from the Publish 
                // objects retrieved above.
                pubEvents.OnPublishBegin += new 
                    _dispPublishEvents_OnPublishBeginEventHandler
                    (this.OnPublishBegin);
                pubEvents.OnPublishDone += new 
                    _dispPublishEvents_OnPublishDoneEventHandler
                    (this.OnPublishDone);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        // When the Publish operation is done, disconnect the event 
        // handlers to prevent slowing of your system.
        public void OnDisconnection(ext_DisconnectMode disconnectMode, 
            ref Array custom)
        {
            if (pubEvents != null)
            {
                pubEvents.OnPublishBegin -= new 
                    _dispPublishEvents_OnPublishBeginEventHandler
                    (this.OnPublishBegin);
                pubEvents.OnPublishDone -= new 
                    _dispPublishEvents_OnPublishDoneEventHandler
                    (this.OnPublishDone);
            }
        }

        // The Publish events.
        public void OnPublishBegin(ref bool pubContinue)
        {
            if (pubContinue == true)
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event is occuring…");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event has halted.");
            }
        }

        public void OnPublishDone(bool success)
        {
            if (success == true)
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event has completed.");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show
                    ("A publish event did not succeed.");
            }
        }
        
        public void OnAddInsUpdate(ref Array custom)
        {
        }

        public void OnStartupComplete(ref Array custom)
        {
        }

        public void OnBeginShutdown(ref Array custom)
        {
        }
        
        private EnvDTE80.PublishEvents pubEvents;
        private DTE2 _applicationObject;
        private AddIn _addInInstance;
    }
}

请参见

概念

控制项目和解决方案

其他资源

响应自动化事件