Visual Studio for Application insider 4
In addition to HostObject, vsa also has a development-friendly feature, Event source Object (how to translate and provide Event objects)
Key Points
First, we need to make it clear that the Event Source Object is added to the script through the AddEventSource of the IVsaCodeItem interface. At runtime, it is obtained through the GetEventSourceInstance of its IVsaSite, and the Event process is customized, is implemented through vsa Ide
I hope the following explanation will help you understand this process. In this process, we use a lot of hostObject code.
Preparation
1. We add an event and method to the document class.
Public Event BeforeSave (ByRef Cancel As Boolean)
Public Sub Save ()
Dim blnCancel As Boolean
RaiseEvent BeforeSave (blnCancel)
If Not blnCancel Then
System. Windows. Forms. MessageBox. Show ("Save document ")
Else
System. Windows. Forms. MessageBox. Show ("do not save document ")
End if
End sub
2. Compile
Vbc document. Vb/T: Library/out: bin \ document. dll/R: system. Windows. Forms. dll
3. Reference Document. dll in the project
4. Locate the myvsasite class and add a savedocument process.
Public sub savedocument ()
Doc. Save ()
End sub
5. Locate the myvsasite class and implement the geteventsourceinstance method.
Here, itemname is the name of ivsacodeitem, and eventsourcename is the first parameter of ivsacodeitem. addeventsource.
Public Function geteventsourceinstance (byval itemname as string, byval eventsourcename as string) as object implements Microsoft. VSA. ivsasite. geteventsourceinstance
Select case itemname. tolower & "+" & eventsourcename. tolower
Case "test + hostobject"
Return Doc
End select
End Function
Next we will describe the key points
How to add an Event Source Object to script
Addeventsource method of ivsacodeitem
This method requires two parameters: the event source object name used in the script and the type name. In our example, we name the object hostobject, type name, of course, document
Dim codeitem as vsacodeitem
Codeitem = m_vsaengine.items.createitem ("test", Microsoft. VSA. vsaitemtype. Code, Microsoft. VSA. vsaitemflag. Class)
Codeitem. addeventsource ("hostobject", "document ")
Instance for retrieving Event soruce Object at runtime
See
Then, we use vsa ide To Add code for this event process.
You can see the automatically generated vsa statement
Public WithEvents HostObject as Document
And the section we added
Save our code, which is consistent with the Code in section 2.
If m_VsaEngine.Compile () Then
Dim pe () As Byte
Dim pdb () As Byte
M_VsaEngine.SaveCompiledState (pe, pdb)
Dim fs As New FileStream ("c: \ test. dll", FileMode. Create)
Dim bs As New BinaryWriter (fs)
Bs. Write (pe)
Fs. Close ()
Bs. Close ()
Fs = New FileStream ("c: \ test. pdb", FileMode. Create)
Bs = New BinaryWriter (fs)
Bs. Write (pdb)
Fs. Close ()
Bs. Close ()
End If
Run
M_VsaEngine = New Microsoft. Vsa. VsaLoader
M_VsaEngine.RootNamespace = "test"
M_VsaEngine.RootMoniker = "test: // project1"
Dim mysite as new myvsasite
M_vsaengine.site = mysite
M_vsaengine.run ()
Mysite. savedocument ()
Check whether the expected results are displayed.
In this example, we use Microsoft. visualBasic. VSA. DT. DLL to display the VSA ide. We will introduce how to display the VSA ide later. below is the code in our example.
Dim mysite as Microsoft. VSA. ivsasite
Mysite = new myvsasite
M_vsaengine = new Microsoft. VisualBasic. VSA. dt. vsaengine
M_vsaengine.name = "engine1"
With m_vsaengine
. RootNamespace = "test"
. RootMoniker = "test: // project1"
. Site = mySite
End
M_VsaEngine.InitNew ()
Dim ReferenceItem As Microsoft. Vsa. IVsaReferenceItem
ReferenceItem = m_VsaEngine.Items.CreateItem ("Document", Microsoft. Vsa. VsaItemType. Reference, Microsoft. Vsa. VsaItemFlag. None)
ReferenceItem. AssemblyName = "C: \ Documents ents and Settings \ Administrator \ My Documents ents \ Visual Studio Projects \ WindowsApplication2 \ WindowsApplication2 \ bin \ Document. Dll"
Dim codeitem as vsacodeitem
Codeitem = m_vsaengine.items.createitem ("test", Microsoft. VSA. vsaitemtype. Code, Microsoft. VSA. vsaitemflag. Class)
Codeitem. addeventsource ("hostobject", "document ")
M_vsadtengine = ctype (m_vsaengine, Microsoft. VSA. ivsadtengine)
M_vsadtengine.initcompleted ()
M_ide = m_vsadtengine.getide
M_ide.site = mysite
If m_vsaengine.compile () then
Try
M_Ide.ShowIDE (True)
Catch ex As Exception
Debug. WriteLine (ex)
End Try
End If