HOW TO:回應 ClickOnce 發行事件
ClickOnce 可以讓您將 Windows 應用程式發行至 Web 伺服器或與簡易安裝共用的網路檔案。 如需詳細資訊,請參閱 ClickOnce 安全性和部署。
Visual Studio 核心 Automation 模型 (包含在 EnvDTE80 中) 有一個名為 PublishEvents 的事件處理物件。 您可以利用此物件偵測 ClickOnce 部署何時開始 (OnPublishBegin) 以及何時結束 (OnPublishDone)。 您可以根據這些事件來執行動作。 在下列程序中,會示範如何處理這些事件。
注意事項 |
---|
根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。使用 [一般開發設定] 開發了這些程序。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。 |
若要回應 ClickOnce 事件
在新的或現有的增益集 (Add-In) 中,將下列程式碼加入至 Connect 類別。
依照 HOW TO:使用增益集管理員來控制增益集中所描述的方式,建置 (Build) 並啟動增益集。
依照 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;
}
}