locked
ActivityBuilder: adding variables programmatically RRS feed

  • Question

  • I'm rehosting WorkflowDesigner and provide a user with a dialog box to add complex variables to the root sequence activity. This works very well if I do it this way:

    // create workflow ...
    wfDesigner = new WorkflowDesigner();
    wfDesigner.Load(new Sequence());
    
    // add variables ...
    ModelService ms = wfDesigner.Context.Services.GetService<ModelService>();
    Variable<T> n = new Variable<T>("MyVariableName");
    n.Default = new VisualBasicValue<T>();
    ((VisualBasicValue<T>)n.Default).ExpressionText = "new MyComplexClass()";
    ms.Root.Properties["Variables"].Collection.Add(n);
    

    Disadvantage of this method is, that I cannot add Arguments to my workflow. Therefore now I try to build my workflow with the ActivityBuilder:

    wfDesigner = new WorkflowDesigner();
    
    activityBuilder = new ActivityBuilder();
    activityBuilder.Implementation = new Sequence()
    {
       Activities = {
         new WriteLine() { Text = "Hello" }
       },
       Variables = { 
         new Variable<string> { Name="sepp", Default="bla" } 
       }
    };
    
    wfDesigner.Load(activityBuilder);

    Now how do I dynamically add variables to the root sequence? What I tried is:

    Variable<T> n = new Variable<T>("MyVariableName", MyDefault);
    (activityBuilder.Implementation as Sequence).Variables.Add(n);
     

    But this crashes as soon as you open the Variables tab in designer. As this article describes, you have to go via the ModelItems (as I did in my working version). So how do I get the ModelItem of my root sequence now? The ModelService.Root is now the ModelItem of ActivityBuilder and I hoped to walk down the ModelItem tree via ModelService.Root.Properties["Activities"].Collection[0]. But there is no "Activities" for the Root (= AcitivityBuilder).

    So how do I get the ModelItem of my root Sequence so that I can add variables as I did before?

    Thanks,
    Günter

    
    Wednesday, August 1, 2012 6:21 AM

Answers

  • Ok, I found the solution myself:

    ModelService ms = wfDesigner.Context.Services.GetService<ModelService>();
    
    var list = ms.Find(ms.Root, typeof(Sequence));
    ModelItem rootSequence = list.First<ModelItem>();

    Of course this needs more checking for multiple or no entries in list, but at least it works.

    Btw. adding not variables to the root sequence, but adding arguments to the workflow can be done like this now:

    ModelItemCollection args = ms.Root.Properties["Properties"].Collection;
    args.Add(new DynamicActivityProperty
    {
      Name = myPara.Name,
      Type = typeof(InArgument<T>),
      Value = new InArgument<T>(myPara) // uses myPara.ToString() for default expression
    });

    Hope this saves somebody some time ...

    • Marked as answer by Gubo Wednesday, August 1, 2012 1:19 PM
    Wednesday, August 1, 2012 1:18 PM