Postupy: Používání objektů BuildManager a BuildManagerEvents
Objekt BuildManager se používá ke správě a zobrazení přenosných spustitelných souborů (PE) vytvořených spuštěním vlastních nástrojů (generátory jednoho souboru), které generují výstup návrhu.Události BuildManagerEvents jsou vyvolány při změně nebo odstranění položek projektu, které vytvářejí dočasné přenositelné spustitelné soubory.
Tento článek podrobně popisuje, jak programovat proti objektu BuildManager a BuildManagerEvents v doplňku Visual Studio.
[!POZNÁMKA]
Váš počítač může zobrazit jiné názvy nebo umístění pro některé prvky uživatelského rozhraní sady Visual Studio v následujících pokynech.Tyto prvky jsou určeny verzí aplikace Visual Studio a použitým nastavením.Další informace naleznete v tématu Přizpůsobení nastavení pro vývoj v sadě Visual Studio.
Chcete-li použít objekty BuildManager a BuildManagerEvents
Vytvořte projekt doplňku Visual Studio pomocí Visual C#.
V nabídce Projekt klepněte na tlačítko Přidat odkaz, klepněte na kartu .NET, vyberte System.Windows.Forms, VSLangProj, VSLangProj2 a VSLangProj80 a potom klepněte na tlačítko OK.
Na začátek souboru Connect.cs přidejte následující příkazy using.
using VSLangProj; using VSLangProj2; using VSLangProj80; using System.Windows.Forms;
Přidejte následující deklaraci k dolní části třídy Připojit pro deklaraci obslužné rutiny BuildManagerEvents.
private DTE2 _applicationObject; private AddIn _addInInstance; private VSLangProj.BuildManagerEvents buildMgrEvents;
Přidejte následující volání metody do metody OnConnection.
_applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Call the BuildMangerSample method. BuildManagerSample(_applicationObject);
Přidáte deklaraci metody BuildManagerSample přímo pod metodu OnConnection.
public void BuildManagerSample(DTE2 dte) { }
Na začátek metody BuildManagerSample přidejte následující deklarace.
Solution2 soln = (Solution2)_applicationObject.Solution; Project proj; VSProject2 vsproj; BuildManager bldMgr;
Přetypujte projekt na objekt VSProject2 přidáním následujícího kódu do metody BuildManagerSample.
proj = soln.Projects.Item(1); // Get a reference to the VSProject2 object. vsproj = (VSProject2)proj.Object;
Přidejte kód k zobrazení zástupných názvů pro soubor PE do pole zprávy pomocí BuildDesignTimeOutput.
bldMgr = vsproj.BuildManager; Array monikers = null; String msg = null; Object obj = bldMgr.DesignTimeOutputMonikers; if (obj != null) { try { monikers = (System.Array)obj; foreach(String tempmoniker in monikers) { msg += bldMgr.BuildDesignTimeOutput(tempmoniker) + "\n"; } } catch(Exception ex) { MessageBox.Show(ex.Message); } MessageBox.Show("The build design-time output is:" + "\n" + msg, "Temporary PE Monikers"); }
Vytvořte obslužné rutiny události BuildManagerEvents přidáním následujícího kódu do metody BuildManagerSample.
//Hook up buildmanager events. buildMgrEvents = vsproj.Events.BuildManagerEvents; buildMgrEvents.DesignTimeOutputDeleted += new _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler (buildMgrEvents_DesignTimeOutputDeleted); buildMgrEvents. DesignTimeOutputDirty += new _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler( buildMgrEvents_DesignTimeOutputDirty);
Přidejte procedury pro každou událost týkající objektu BuildManagerEvents.
void buildMgrEvents_DesignTimeOutputDirty (string bstrOutputMoniker) { MessageBox.Show(bstrOutputMoniker + " is dirty." , "BuildManager Events"); } void buildMgrEvents_DesignTimeOutputDeleted (string bstrOutputMoniker) { MessageBox.Show(bstrOutputMoniker + " was deleted." , "BuildManager Events"); }
Konečně zakažte obslužné rutiny události přidáním následujícího kódu do metody OnDisconnection.
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { // If the delegate handlers have been connected, then // disconnect them here. // If you do not do this, the handlers may still // fire because garbage collection has not removed them. if (buildMgrEvents != null) { buildMgrEvents.DesignTimeOutputDeleted -= new _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler (buildMgrEvents_DesignTimeOutputDeleted); buildMgrEvents.DesignTimeOutputDirty -= new _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler (buildMgrEvents_DesignTimeOutputDirty); } }
Úplný kód je vypsán v oddíle Příklad tohoto tématu.
Pokud chcete sestavit doplněk, klikněte na Sestavit řešení v nabídce Sestavení.
Otevřete projekt Visual C# nebo Visual Basic v integrovaném vývojovém prostředí (IDE) Visual Studio.
Pokud chcete do projektu přidat datovou sadu, klikněte na tlačítko Přidat novou položku v nabídce Projekt.Vyberte DataSet z dialogového okna Přidat novou položku a klikněte na tlačítko OK.
Soubor DataSet zajišťuje, že projekt má vlastní nástroj (jeden generátor souborů) s ním spojený.
V nabídce Nástroje klepněte na tlačítko Správce doplňků a vyberte doplněk z dialogového okna Správce doplňků.Klepněte na tlačítko OK a spusťte tak doplněk.
Chcete-li otestovat kód BuildManagerEvents
Chcete-li zobrazit vyvolávání obslužných rutin BuildManagerEvents, přidejte novou datovou sadu do projektu, změňte vlastnosti souboru datové sady nebo odstraňte soubor datové sady.
Změna vlastností souboru datové sady:
Zvolte soubor datové sady v Průzkumníku řešení.
Klepněte pravým tlačítkem na soubor a vyberte z rozevírací nabídky možnost Vlastnosti.
V okně Vlastnosti upravte libovolné pole.
Odstranění datové sady:
Zvolte soubor datové sady v Průzkumníku řešení.
Klepněte pravým tlačítkem na soubor a vyberte z rozevírací nabídky možnost Odstranit.
Příklad
Následující příklad je základní doplněk Visual Studio, který ukazuje, jak používat objekty BuildManager a BuildManagerEvents pomocí automatizace Visual Studio.
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using System.Windows.Forms;
namespace MyAddIn
{
public class Connect : Object, IDTExtensibility2
{
public Connect()
{
}
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
// Call the BuildMangerSample method.
BuildManagerSample(_applicationObject);
}
public void BuildManagerSample(DTE2 dte)
{
try
{
Solution2 soln =
(Solution2)_applicationObject.Solution;
Project proj;
VSProject2 vsproj;
BuildManager bldMgr;
proj = soln.Projects.Item(1);
// Cast to the VSProject2 object.
vsproj = (VSProject2)proj.Object;
bldMgr = vsproj.BuildManager;
Array monikers = null;
String msg = null;
Object obj = bldMgr.DesignTimeOutputMonikers;
if (obj != null)
{
try
{
monikers = (System.Array)obj;
foreach(String tempmoniker in monikers)
{
msg +=
bldMgr.BuildDesignTimeOutput(tempmoniker) + "\n";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("The build design-time output is:"
+ "\n" + msg, "Temporary PE Monikers");
}
//Hook up buildmanager events.
buildMgrEvents = vsproj.Events.BuildManagerEvents;
buildMgrEvents.DesignTimeOutputDeleted +=new
_dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
(buildMgrEvents_DesignTimeOutputDeleted);
buildMgrEvents.DesignTimeOutputDirty +=new
_dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler
(buildMgrEvents_DesignTimeOutputDirty);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void buildMgrEvents_DesignTimeOutputDirty
(string bstrOutputMoniker)
{
MessageBox.Show(bstrOutputMoniker + " is dirty.",
"BuildManager Events");
}
void buildMgrEvents_DesignTimeOutputDeleted(
string bstrOutputMoniker)
{
MessageBox.Show(bstrOutputMoniker + " was deleted."
, "BuildManager Events");
}
public void OnDisconnection(ext_DisconnectMode disconnectMode
, ref Array custom)
{
// If the delegate handlers have been connected, then
// disconnect them here.
// If you do not do this, the handlers may still
// fire because garbage collection has not removed them.
if (buildMgrEvents != null)
{
buildMgrEvents.DesignTimeOutputDeleted -= new
_dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
(buildMgrEvents_DesignTimeOutputDeleted);
buildMgrEvents.DesignTimeOutputDirty -= new
_dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler
(buildMgrEvents_DesignTimeOutputDirty);
}
}
public void OnAddInsUpdate(ref Array custom)
{
}
public void OnStartupComplete(ref Array custom)
{
}
public void OnBeginShutdown(ref Array custom)
{
}
private DTE2 _applicationObject;
private AddIn _addInInstance;
private VSLangProj.BuildManagerEvents buildMgrEvents;
}
}
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80
Public Class Connect
Implements IDTExtensibility2
Dim _applicationObject As DTE2
Dim _addInInstance As AddIn
Public WithEvents buildMgrEvents As VSLangProj.BuildManagerEvents
Public Sub New()
End Sub
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
BuildManagerSample(_applicationObject)
End Sub
Sub BuildManagerSample(ByVal dte As DTE2)
Try
Dim soln As Solution2 = CType(_applicationObject.Solution _
, Solution2)
Dim proj As Project
Dim vsproj As VSProject2
Dim bldMgr As BuildManager
proj = soln.Projects.Item(1)
' Cast the project to a VSProject2.
vsproj = CType(proj.Object, VSProject2)
bldMgr = vsproj.BuildManager
Dim monikers As String() = Nothing
Dim moniker As String = Nothing
Dim msg As String = ""
Dim obj As Object = bldMgr.DesignTimeOutputMonikers
If Not obj Is Nothing Then
Try
monikers = CType(obj, String())
For Each moniker In monikers
msg &= bldMgr.BuildDesignTimeOutput(moniker) _
+ vbCr
Next
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
MsgBox("The build design-time output is:" + vbCr _
+ msg, MsgBoxStyle.Information _
, "BuildManager Monikers")
End If
buildMgrEvents = vsproj.Events.BuildManagerEvents
AddHandler buildMgrEvents.DesignTimeOutputDeleted _
, AddressOf OutputDeleted
AddHandler buildMgrEvents.DesignTimeOutputDirty _
, AddressOf OutputDirty
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub
Sub OutputDeleted(ByVal deletedMoniker As String)
MsgBox(deletedMoniker & " was deleted." _
, MsgBoxStyle.Information, "BuildManagerEvents Information")
End Sub
Sub OutputDirty(ByVal dirtyMoniker As String)
MsgBox(dirtyMoniker & " is dirty." _
, MsgBoxStyle.Information, "BuildManagerEvents Information")
End Sub
Public Sub OnDisconnection(ByVal disconnectMode _
As ext_DisconnectMode, ByRef custom As Array) _
Implements IDTExtensibility2.OnDisconnection
' Turns off BuildManager event handling when the
' add-in shuts down.
buildMgrEvents = Nothing
End Sub
Public Sub OnAddInsUpdate(ByRef custom As Array) _
Implements IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnStartupComplete(ByRef custom As Array) _
Implements IDTExtensibility2.OnStartupComplete
End Sub
Public Sub OnBeginShutdown(ByRef custom As Array) _
Implements IDTExtensibility2.OnBeginShutdown
End Sub
End Class
Probíhá kompilace kódu
Chcete-li tento kód zkompilovat, vytvořte nový projekt doplňku Visual Studio a nahraďte kód ve třídě Připojit kódem v příkladu.Před spuštěním doplňku otevřete projekt Visual C# nebo Visual Basic v aplikaci Visual Studio IDE.Další informace o spuštění doplňku naleznete v části Postupy: Řízení doplňků pomocí Správce doplňků.
Viz také
Koncepty
Představení objektu BuildManager