ShapeAppDynamicProgrammingModelCSharp_ProxyShim Sample

Summit now offers the he ShapeAppDynamicProgrammingModelCSharp_ProxyShim sample which is based both the VSTA 2.0 SDK sample ShapeAppDyanmicProgrammingModelCSharp and the Microsoft endorsed ShapeAppMacroRecordingCSharp-NoRuntime sample.  This sample references the VSTA runtime and uses the auto-generated designer files for the Dynamic Programming Model (DPM) add-ins; however, the VSTA runtime is not used to load the add-ins.  Instead a host type, IAddIn, is used along with AssemblyLoad and a customized ProxyShim to load the DPM add-ins.  The ProxyShim references the host application directly and contains type definitions only for the entry point types.  These definitions have been modified for use with the IAddIn interface.  All changes have been marked with a comment “SUMMIT CHANGE” and explanation.  To access this sample, please fill out the information form, then e-mail vstasupport@summsoft.com with your username and request.

 

Use of IAddIn:
As in the NoRuntime sample, the host application in this sample defines the interface IAddIn to use in much the same way that the VSTA Runtime’s IEntryPoint and IMultipleEntryPoint are used in SDK samples, mainly to start and stop the add-in.  For NoRuntime and ProxyShim style integrations an additional file is required in the add-ins which implements the IAddIn interface.  In this sample, that code file is called “IAddIn_Implementation” and it contains methods to instantiate all host items as well as the host item container, namely the host item container “ThisDocument.xx” and the host items “Drawing#.xx”.  In order to keep the DPM add-in in sync with the host this file is regenerated after any host item additions or removals.  Hence, because the file is auto-generated and is regenerated during design time, this file should not be modified.

 

Improvements over the SDK DPM Sample:
The SDK DPM sample ShapeAppDynamicProgrammingModelCSharp allows for host objects within host items (in this case shapes within a drawing) to be referenced through local variables; however, host items within the host item container (in this case drawings within a document) cannot be referenced in the same manner.  Since the ProxyShim style integration is not limited by the VSTA runtime it is possible to reference host items through a named variable within the host item container.    See code sample below which contains the startup methods for a host item (Drawing1_Startup) and for the host item container (ThisDocument_Startup).

Private Sub Drawing1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
     'refer to a host object by its variable name
     Me.Circle1.Size = New Size(25, 25)
  End Sub
 
Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    'CANNOT refer to a host item through a variable name
    ' using an SDK style integration, 
    'CAN refer to a host item through a variable name with the
    'ProxyShim style integration
    Me.Drawing1.Circle1.Location = New Point(100, 100)
End Sub
 
private void Drawing1_Startup(object sender, System.EventArgs e)
{
    //refer to a host object by its variable name
    this.Circle1.Size = new Size(50, 75);
}
 
private
void ThisDocument_Startup(object sender, EventArgs e)
{
    //CANNOT refer to a host item through a variable name
    // using an SDK style integration, 
    //CAN refer to a host item through a variable name with the
    //ProxyShim style integration
    this.Drawing1.Circle1.Location = new Point(75, 75);
}

 

Additionally, like the NoRuntime style integration the ProxyShim style integration supports the “new” operator.  To use this feature with the ShapeApp samples, add public constructor methods to the desired classes.  For the ShapeAppDynamicProgrammingModelCSharp_ProxyShim sample, the Drawing class was modified changing the constructor from private to public.  Another code change was required to the VSTADesignTimeIntegration class; the code to build an existing project when opening was moved from the EnsureIDE method to the end of the Connect method to avoid building prior to hooking-up any host items added with the “new” operator in the add-in.  The code sample below demonstrates use of this modification and the “new” operator.

Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
 
    'calculate a valid drawing name
    Dim name As String = "Drawing" & (Me.Drawings.Count + 1).ToString()
 
    'create a new drawing and add it to the document
    Dim d As New Drawing(Me.Application, name)
    Me.Drawings.Add(d)
 
End Sub
 
private void ThisDocument_Startup(object sender, EventArgs e)
{
    //calculate a valid name for the drawing
    string name = "Drawing" + (this.Drawings.Count + 1).ToString();
 
    //create the new drawing and add it to this document
    Drawing d = new Drawing(this.Application, name);
    this.Drawings.Add(d);
}
 


Prerequisites:  
To build and run the ShapeAppDynamicProgrammingModelCSharp_ProxyShim sample, you must first install the Microsoft VSTA 2.0 SDK, available from Summit Software .  You must also have Visual Studio 2008 or higher installed.

Running the Sample:  
Follow these instructions to run the sample.

1)      Extract the sample to C:\ShapeAppSamples.  If a different location is used, then the references to the host in the DPM add-ins and templates must be updated.

2)      Run the included setup file SetupShapeAppDynamicProgrammingModelCSharp_PS.js.  This will setup the ShapeAppCSharp host and register both the project templates and the item templates which are required for DPM add-ins.

3)      Open and build the ShapeAppDynamicProgrammingModelCSharp_PS solution to create the exe which the DPM ProxyShim and add-ins will reference.

4)      Run the sample either through Visual Studio or directly from the exe.

5)      In the ShapeApp application, save the document and add a C# or VB DPM add-in project through the customization menu.

6)      In the VSTA IDE open the IAddIn_Implementation file which contains a partial class for the host item container (ThisDocument) as well as implementation code for the IAddIn interface and setup code for local variables.  Each local variable corresponds to a host item (Drawing1, Drawing2, and Drawing3).

7)      In the ShapeApp host application delete Drawing1 and add a new Drawing.

8)      In the IAddIn_Implementation file note that the code has been regenerated to stay in sync with the host items.  Now local variables should be present for Drawing2, Drawing3, and Drawing4.

9)      In the ShapeApp host application add a shape Drawing2.  The VSTA runtime will automatically add code to the designer file, Drawing2.designer.xx, to declare a variable to correspond to this host object.

10)   In the VSTA IDE select the code file for Drawing2 and add code to manipulate the shape.  For example:

Private Sub Drawing2_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
     Me.Circle1.Size = New Size(25, 25)
End Sub
 
private void Drawing2_Startup(object sender, System.EventArgs e)
{
    //refer to a host object by its variable name
    this.Circle1.Size = new Size(25, 25);
}
 

11)   In the code file for the host item container (ThisDocument.xx), add code to manipulate the shape added to Drawing2 through the local variable for Drawing2.  Add code to create a new drawing using the “new” operator and add it to the document.  For example: 

Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    Me.Drawing2.Circle1.Location = New Point(100, 100)
 
           'calculate a valid drawing name
          Dim name As String = "Drawing" & (Me.Drawings.Count + 1).ToString()
 
           'create a new drawing and add it to the document
          Dim d As New Drawing(Me.Application, name)
          Me.Drawings.Add(d)
 
End Sub
 
private void ThisDocument_Startup(object sender, EventArgs e)
{
    this.Drawing2.Circle1.Location = new Point(75, 75);
 
    //calculate a valid name for the drawing
    string name = "Drawing" + (this.Drawings.Count + 1).ToString();
 
    //create the new drawing and add it to this document
    Drawing d = new Drawing(this.Application, name);
    this.Drawings.Add(d);
}

 

12)   Build the add-in project, then save the document through the host application.

13)   Close and re-open the document and to launch the code added above.


Posted Oct 19 2010, 02:15 PM by Melody
Copyright Summit Software Company, 2008. All rights reserved.