none
Can IActivityTemplateFactory be used to add variables?

    Question

  • Hello Everyone,

         I just started looking into the IActivityTemplateFactory interface and was wondering if I could use it to add the variables the activity may need to bind to.  If this functionality isn't available in beta 2, it might be worth considering for a future release.

    Thanks in advance,

    Bob
    Sunday, December 06, 2009 10:03 PM

Answers

  • Bob,

    IActivityTemplateFactory lets you specify what you want created (and that includes things like Variables as well as activities.  You need have a place to put them, however.  The If activity, for instance, does not have a Variables collection on it that you can add something to. Parallel, Sequence, Flowchart, DoWhile and others do have a Variables collection. 

    The variables are added wherever you placed them (in Leon's case, he is adding them to sequence).

    Let me know if that helps,

    matt
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    Tuesday, December 08, 2009 8:23 PM

All replies

  • This can be done in beta2. If you declare a variable in your IActivityTemplateFactory and use it, it will be added to the variables in the workflow where you add the activity:

    public class MyActWithVars : IActivityTemplateFactory
    {
        public Activity Create(System.Windows.DependencyObject target)
        {
            var stringVar = new Variable<string>() { Name = "stringVar", Default = "Hello World" };            
            return new Sequence
            {
                Variables = { stringVar },
                Activities = 
                {                   
                    new WriteLine
                    {
                        Text = new VisualBasicValue<string>("stringVar")
                    }
                }
            };            
        }
    }

    This is not restricted to only one variable, you can have as many variables as you want


    public class MyActWithVars : IActivityTemplateFactory
    {
        public Activity Create(System.Windows.DependencyObject target)
        {
            var stringVar = new Variable<string>() { Name = "stringVar", Default = "Hello World" };
            var stringVar2 = new Variable<string>() { Name = "stringVar2", Default = "Good bye World" };
            var stringVar3 = new Variable<string>() { Name = "stringVar3", Default = "Another var" };            
            return new Sequence
            {
                Variables = { stringVar, stringVar2, stringVar3 },
                Activities = 
                {       
                    // Add your activites here...
                }
            };            
        }
    }
    Leon Welicki
    Program Manager, WF
    Monday, December 07, 2009 8:17 PM
  • Leon,

         Wow, you workflow guys/girls really put a lot of thought into the 4.0 version.  This particular feature makes my life a lot easier now.  Keep up the good work!

    Bob
    Monday, December 07, 2009 9:13 PM
  • Maybe I spoke too soon.  I just noticed you are using a Sequence activity in your example.  What if I wanted to use the If activity instead?  The variable should be added in the next available scope by default.  Is this how it works now?

    Bob
    Tuesday, December 08, 2009 6:57 PM
  • Bob,

    IActivityTemplateFactory lets you specify what you want created (and that includes things like Variables as well as activities.  You need have a place to put them, however.  The If activity, for instance, does not have a Variables collection on it that you can add something to. Parallel, Sequence, Flowchart, DoWhile and others do have a Variables collection. 

    The variables are added wherever you placed them (in Leon's case, he is adding them to sequence).

    Let me know if that helps,

    matt
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    Tuesday, December 08, 2009 8:23 PM
  • Matt,

    I'm just trying to figure out where the next available variables collection is up the activity tree and then add my variable to it, in the case of activities which don't have their own variables collection.  Judging from the answers I've been getting, this is going to require reflection.  If so, how can I get the next available activity up the tree which has a variables collection?  How do I add my variables to it?

    Bob
    Tuesday, December 08, 2009 8:45 PM
  • Bob,

    I don't have the code on my machine right now, but if you look at the object passed to the create method, it should probably a workflowViewElement or something like that you can use to get access to the ModelItem tree which will let you start walking the tree.  I think Kushal has a post coming on that topic at some point in time, I'll ping him to see if he can respond to this thread (or post it to his blog :-) )

    matt
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    • Proposed as answer by Calvin Charles Friday, August 24, 2012 11:10 AM
    Tuesday, December 08, 2009 10:46 PM
  • Matt,

        I didn't realize I was able to get to the model tree that way, that is great info to know.  I think I could probably work it out on my own, but if Kushal wants to post another one of his super awesome blog posts that would be great to.  Either way, I'll post the answer here for posterity, unless someone else beats me to it.  Thanks again for your help!

    Bob
    Tuesday, December 08, 2009 11:00 PM
  • Matt,

         There is something wrong with setting the To and Value properties here, and I'm sure its something small.  You wouldn't happen to know what it is would you?

    return new Assign
                {
                    To = new VisualBasicValue<string>("System_String_1"),
                    Value = new VisualBasicValue<string>("System_String_2")
                };
    Bob
    Wednesday, December 09, 2009 3:34 AM
  • return new Assign
                {
                    To = new VisualBasicValue<string>("System_String_1"),
                    Value = new VisualBasicValue<string>("System_String_2")
                };

    Hopefully this is long-solved, but:

    var t = new Assign
    {
        To = new OutArgument<string>()
        {
            Expression = new VisualBasicReference<string>("System_String_1")
        },
        Value = new InArgument<string>()
        {
            Expression = new VisualBasicValue<string>("System_String_2")
        },
    };





    Friday, January 22, 2010 9:52 PM