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
, .
:
private Variable<string> _text;
_text = new Variable<string>(InputName);
metadata.AddImplementationVariable(_text);
var retval = new Parent()
{
Input = new InArgument<string>()
};
retval.Variables.Add(retval._text);
return retval;
. , . . :
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);
}
. , .