10/08/2012

Building a Composite Activity Designer part(3 - 4)


Part 3  Registering the Activities Designers
  1. Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  2. Open the project from last post Building a Composite Activity Designer part(2 - 4) .
  3. Register the PrePostSequence and TryCatch designers in the Workflow Designer metadata. To do this, in Solution Explorer, under the HostingApplication project, right-click MainWindow.xaml and select View Code. In the RegisterMetadata method add the following code (shown in bold):
    C#
    private void RegisterMetadata()
    {
        (new DesignerMetadata()).Register();

        AttributeTableBuilder builder = new AttributeTableBuilder();

        // Register Custom Designers.
        builder.AddCustomAttributes(typeof(Prompt), new DesignerAttribute(typeof(BasicDesignerWithStyling)));
       
        builder.AddCustomAttributes(typeof(PrePostSequence), new DesignerAttribute(typeof(PrePostSequenceDesigner)));
        builder.AddCustomAttributes(typeof(TryCatch), new DesignerAttribute(typeof(MyTryCatchFinally)));

        // Apply the metadata
        MetadataStore.AddAttributeTable(builder.CreateTable());
    }
    Visual Basic
    Private Sub RegisterMetadata()
        Dim dm As New DesignerMetadata()
        dm.Register()

        Dim builder As New AttributeTableBuilder()

        ' Register Custom Designers.
        builder.AddCustomAttributes(GetType(Prompt), New DesignerAttribute(GetType(BasicDesignerWithStyling)))
       
        builder.AddCustomAttributes(GetType(PrePostSequence), New DesignerAttribute(GetType(PrePostSequenceDesigner)))
        builder.AddCustomAttributes(GetType(TryCatch), New DesignerAttribute(GetType(MyTryCatchFinally)))
       
        ' Apply the metadata
        MetadataStore.AddAttributeTable(builder.CreateTable())
    End Sub
    Note: Doing this you are overriding the default TryCatch designer with the custom one created in the previous post.
  4. Create two new toolbox items for each activity, and register them in the designer Toolbox. To do this, find the GetToolboxControl method and add the following code (shown in bold):
    C#
    private ToolboxControl GetToolboxControl()
    {
        //Create the ToolBoxControl
        ToolboxControl ctrl = new ToolboxControl();

        //Create a collection of categories
        ToolboxCategory categoryItems = new ToolboxCategory("category1");

        //Creating toolboxItems
        ToolboxItemWrapper tool = new ToolboxItemWrapper("CustomActivities.Prompt",
    "CustomActivities", null, null);

        ToolboxItemWrapper tool2 = new ToolboxItemWrapper("System.Activities.Statements.Sequence",
            "System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
            null, "Sequence");

        ToolboxItemWrapper tool3 = new ToolboxItemWrapper("System.Activities.Statements.TryCatch", "System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", null, "MyTryCatchFinally");
        ToolboxItemWrapper tool4 = new ToolboxItemWrapper("CustomActivities.PrePostSequence", "CustomActivities", null, "PrePostSequence");

        categoryItems.Add(tool);
        categoryItems.Add(tool2);
        categoryItems.Add(tool3);
        categoryItems.Add(tool4);

        ctrl.Categories.Add(categoryItems);
        return ctrl;
    }
    Visual Basic
    Private Function GetToolboxControl() As ToolboxControl
        ' Create the ToolBoxControl
        Dim ctrl As New ToolboxControl()

        ' Create a collection of categories
        Dim categoryItems As New ToolboxCategory("category1")
            
        'Creating toolboxItems
        Dim tool As New ToolboxItemWrapper("CustomActivities.Prompt", "CustomActivities", Nothing, Nothing)

        Dim tool2 As New ToolboxItemWrapper("System.Activities.Statements.Sequence", "System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", Nothing, "Sequence")

        Dim tool3 As New ToolboxItemWrapper("System.Activities.Statements.TryCatch", "System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", Nothing, "MyTryCatchFinally")
        Dim tool4 As New ToolboxItemWrapper("CustomActivities.PrePostSequence", "CustomActivities", Nothing, "PrePostSequence")

        categoryItems.Add(tool)
        categoryItems.Add(tool2)
        categoryItems.Add(tool3)
        categoryItems.Add(tool4)

        ctrl.Categories.Add(categoryItems)
        Return ctrl
    End Function


No comments:

Post a Comment