Activity can't set a variable defined within its scope?

It makes me scratch my head, and I wonder if I get it right.

I am trying to define a variable in Activity that can be used by child actions. The parent should be able to set the value in this variable at runtime.

Here's the trimmed parent class:

public sealed class Parent : NativeActivity, IActivityTemplateFactory
{
    public Collection<Child> Children { get; private set; }
    private CompletionCallback<string> _cc;
    private Variable<int> _index;
    public Collection<Variable> Variables { get; private set; }        
    public Parent()
    {
        Children = new Collection<Child>();
        _index = new Variable<int>();
        _cc = new CompletionCallback<string>(OnComplete);
        Variables = new Collection<Variable>();
    }
    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        base.CacheMetadata(metadata);
        metadata.AddImplementationVariable(_index);
        metadata.SetVariablesCollection(Variables);
    }
    protected override void Execute(NativeActivityContext context)
    {
        _index.Set(context, 0);
        var input = Variables.First(x => x.Name == "ThisIsTheVariable");
        input.Set(context, "This is the Value");
        context.ScheduleActivity(Children[0], _cc);
    }
    private void OnComplete(NativeActivityContext context, ActivityInstance instance, string result)
    {
        var index = _index.Get(context) + 1;
        _index.Set(context, index);
        Console.WriteLine(result);
        if (index < Children.Count)
            context.ScheduleActivity(Children[index], _cc);
    }
    Activity IActivityTemplateFactory.Create(System.Windows.DependencyObject target)
    {
        var retval = new Parent();
        retval.Variables.Add(new Variable<string>("ThisIsTheVariable"));
        return retval;
    }
}

Now, if I add this to the workflow and run it, I get the following very ironic exception:

Activity “1.1: Parent” cannot access this variable because it is declared in scope 1.1: Parent. ”Activity can only be its own implementation variables.

, , . , , , intellisense . , , , , OutArgument . - .

?


Edit

, .

:

// declaration
private Variable<string> _text;

// in ctor I create it
_text = new Variable<string>(InputName);

// in CacheMetadata I declare it an implementation variable
metadata.AddImplementationVariable(_text);

// in IActivityTemplateFactory.Create I add it to Variables
var retval = new Parent()
{
    Input = new InArgument<string>()
};
retval.Variables.Add(retval._text);
return retval;

. , . . :

// set text via the implementation variable
protected override void Execute(NativeActivityContext context)
{
    _text.Set(context, "DERP");
    context.ScheduleActivity(Children[0], _cc);
}

, DERP, , , -, _text, null.

- Variables .

protected override void Execute(NativeActivityContext context)
{
    Variables.First().Set(context, "DERP");
    context.ScheduleActivity(Children[0], _cc);
}

. , .

+3
2
+1

, , , - ...

, context.DataContext.

;

var input = Variables.First(x => x.Name == "ThisIsTheVariable"); 
input.Set(context, "This is the Value");

;

var input = context.DataContext.GetProperties()["ThisIsTheVariable"];
input.SetValue(context.DataContext, "This is the Value");
+1

All Articles