Workflows in VSTA

Q:

 

Do you have any ideas/examples insight on how to implement VSTA 2.0 w/ WPF/Workflows?  (Particularly the CodeActivity Activity)

 

 

A:

 

If it is an event you are trying to hook into, you have two options since you are using a minimal integration:
1)      Expose the event directly
2)      Use an alternative event

Considerations for these options:
1)      Expose the event directly

You want to expose your Object Model, not user interface or similar pieces of your application.  It seems like a workflow would be an implementation of your object model, not an abstraction of it.  Unless this is actually part of your object model you do not want to expose it.  Instead abstract the piece of your Object Model which is handled by this Workflow and use an alternative event.
If you do want to expose this directly and it's public, there shouldn't be any problem because you are referencing your application directly.

2)      Use an alternative event

Instead of exposing an implementation of your object model (for instance a button or a form or possibly a workflow) use a public event/event args which represent the part of your object model you are actually trying to expose.  For instance, if a user clicks a "RUN" button you wouldn't want VSTA to hook into this.Application.Form.cmdRun.Click, instead you would want them to hook into an event like this.Application.RunStart which would be raised in the method hooked into the this.Application.Form.cmdRun.Click (see the example below).  This could be done the same way for a workflow:


In main_frm.vb:

      Private Sub Output_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles Output.KeyPress
            Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
MainDoc.RaiseKeyPressEvent(KeyAscii)
            eventArgs.KeyChar = Chr(KeyAscii)
            If KeyAscii = 0 Then
                  eventArgs.Handled = True
            End If
End Sub

In Document:

Event KeyPress(ByRef KeyCode As Short)

'This routine is called to tell VSTA that the event occurred
Friend Sub RaiseKeyPressEvent(ByRef KeyCode As Short)
RaiseEvent KeyPress(KeyCode)
End Sub


 In VSTA:

Private Sub AppAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
Me.Application.Document.Text &= ":)"
AddHandler Me.Application.Document.KeyPress, AddressOf HandleKeyPress
End Sub


Private Sub HandleKeyPress()
'do something
End Sub

 

 

 


Posted May 28 2009, 11:52 AM by BillL
Filed under: ,
Copyright Summit Software Company, 2008. All rights reserved.