مشاركة عبر


كيفية القيام بما يلي: استخدام BuildManager والكائنات BuildManagerEvents

BuildManagerيتم استخدام كائن إدارة وعرض الملفات (PE) القابلة للتنفيذ محمول المنتجة من قبل تشغيل أدوات مخصصة (ملف فردي generators) بإنشاء إخراج وقت التصميم. BuildManagerEventsيتم رفع أحداث عندما يتم تم تغييره عناصر المشروع إنشاء الملفات التنفيذية محمول المؤقت أو تم الحذفها.

التفاصيل التالية كيفية إلى البرنامج مقابل BuildManagerو BuildManagerEventsالكائن في Visual Studioإضافة بوصة

ملاحظة

قد تختلف مربعات الحوار وأوامر القائمة التى تشاهدها الان عن تلك الموصوفة في التعليمات اعتماداً على الإعدادات النشطة أو الإصدار الخاص بك. تم تطوير هذه الإجراءات من خلال "إعدادات تطوير عام" النشط. لتغيير الإعدادات الخاصة بك, اختر إعدادات الاستيراد و التصدير ضمن القائمة أدوات . لمزيد من المعلومات، راجع العمل مع إعدادات.

استخدام BuildManager والكائنات BuildManagerEvents

  1. Visual Studioإضافة-in مشروع باستخدام إنشاء #Visual C.

  2. على القائمة مشروع، انقر فوق إضافة مرجعوانقر فوق .NET علامة تبويب تحديد النظام.Windows.Forms VSLangProj VSLangProj2 VSLangProj80و انقر فوق ‏‏موافق .

  3. إضافة التالية استخدام عبارات إلى أعلى الملف يعيّن.cs.

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using System.Windows.Forms;
    
  4. إضافة تعريف التالية في الجزء أسفل من فئة يعيّن التصريح BuildManagerEventsمعالج.

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private VSLangProj.BuildManagerEvents buildMgrEvents;
    
  5. إضافة استدعاء الأسلوب التالي إلى أسلوب OnConnection.

    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Call the BuildMangerSample method.
    BuildManagerSample(_applicationObject);
    
  6. إضافة تعريف أسلوب CSVSProj2Manip مباشرةً أسفل أسلوب OnConnection.

    public void BuildManagerSample(DTE2 dte)
    {
    }
    
  7. قم بإضافة سطر التعليمة البرمجية التالية أعلى الملف.

    Solution2 soln = (Solution2)_applicationObject.Solution;
    Project proj;
    VSProject2 vsproj;
    BuildManager bldMgr;
    
  8. تحويل إلى كائن VSProject2 المشروع عن طريق إضافة التالية تعليمات برمجية لأسلوب BuildManagerSample.

    proj = soln.Projects.Item(1);
    // Get a reference to the VSProject2 object.
    vsproj = (VSProject2)proj.Object;
    
  9. قم بإضافة تعليمات برمجية إلى عرض الألقاب ملف PE في مربع رسالة باستخدام 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");
    }
    
  10. إنشاء BuildManagerEventsمعالجات الأحداث بإضافة التالية تعليمات برمجية إلى أسلوب BuildManagerSample.

    //Hook up buildmanager events.
    buildMgrEvents = vsproj.Events.BuildManagerEvents;
    buildMgrEvents.DesignTimeOutputDeleted +=
    new _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
    (buildMgrEvents_DesignTimeOutputDeleted);
    buildMgrEvents. DesignTimeOutputDirty +=
    new _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler(
    buildMgrEvents_DesignTimeOutputDirty);
    
  11. إضافة إجراءات لكل حدث المتعلقة إلى كائن الحدث.

    void buildMgrEvents_DesignTimeOutputDirty
    (string bstrOutputMoniker)
    {
        MessageBox.Show(bstrOutputMoniker + " is dirty."
    , "BuildManager Events");
    }
    
    void buildMgrEvents_DesignTimeOutputDeleted
    (string bstrOutputMoniker)
    {
        MessageBox.Show(bstrOutputMoniker + " was deleted."
    , "BuildManager Events");
    }
    
  12. وأخيراً، قم بيعطل معالجة الحدث عن طريق إضافة التالية تعليمات برمجية لأسلوب 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);
        }
    }
    

    يتم إدراج رمز كاملة في المقطع المثال الخاص بهذا الموضوع.

  13. إلى إنشاء الوظيفة الإضافية وانقر فوق بنية الحل بنية قائمة.

  14. إفتح المشروع في بيئة التطوير المتكاملة (IDE).

  15. لإضافة مجموعة بيانات المشروع انقر فوق إضافة عنصر جديد تشغيل القائمة المشروع. تحديد DataSet من مربع حوار إضافة عنصر جديد ثم انقر فوق ‏‏موافق .

    يضمن ملف DataSet يحتوي المشروع على أداة مخصصة (ملف فردي مولد) المقترن مع عليه.

في القائمة أدوات، انقر فوق إضافة - إدارة وتحديد الخاص بك إضافة - من إضافة - في إدارة مربع حوار. انقر فوق ‏‏موافق إلى تشغيل الوظيفة الإضافية.

إلى اختبار تعليمات برمجية BuildManagerEvents

  • لمشاهدة BuildManagerEventsإضافة مجموعة بيانات جديدة إلى المشروع أو تعديلها من خصائص الملف مجموعة البيانات معالجات تشغيل, أو يحذف مجموعة بيانات الملف.

    إلى تعديل خصائص الملف مجموعة البيانات:

    1. في مستكشف الحلول ، حدد ملف الصورة النقطية.

    2. يمين-انقر تشغيل ملف ثم تحديد خصائص من المنسدلة - لأسفل القائمة.

    3. ضمن النافذة خصائص تعديل أي من الحقول.

    إلى يحذف ورقة العمل:

    1. في مستكشف الحلول ، حدد ملف الصورة النقطية.

    2. يمين-انقر تشغيل ملف ثم تحديد خصائص من المنسدلة - لأسفل القائمة.

مثال

المثال التالي هو أساسية Visual Studioإضافة - في هذا يوضح كيفية استخدام BuildManagerو BuildManagerEventsالكائنات باستخدام 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

التحويل البرمجي للتعليمات البرمجية

إلى يحول برمجياً هذه التعليمة البرمجية إنشاء جديد Visual Studioإضافة-in المشروع واستبدالها رمز أسلوب OnConnection مع تعليمات برمجية الموجودة في المثال. قبل تشغيل الوظيفة الإضافية قم بفتح #Visual Cأو Visual Basicمشروع في Visual StudioIDE. لمزيد من المعلومات حول كيفية إلى تشغيل وظيفة الإضافية "، راجع كيفية: عنصر تحكم الوظائف الإضافية مع إدارة الوظائف الإضافية.

راجع أيضًا:

المبادئ

مقدمة إلى كائن BuildManager

مقدمة إلى مشروع الامتداد

موارد أخرى

التنفيذ التلقائي "و" الامتداد لبرنامج Visual Studio