我在复制器活动中有一个调用工作流活动。我尝试调用的工作流需要向其传递 2 个参数,一个整数和一个字符串参数,这些参数应由复制器活动传递给工作流。关于如何做到这一点的任何想法?
谢谢。
我在复制器活动中有一个调用工作流活动。我尝试调用的工作流需要向其传递 2 个参数,一个整数和一个字符串参数,这些参数应由复制器活动传递给工作流。关于如何做到这一点的任何想法?
谢谢。
这是一个完整的示例(请注意,可以在设计器的属性窗格中设置构造函数中包含的任何内容): Workflow3 是仅包含 CodeActivity 的目标工作流,后面的代码如下:
public sealed partial class Workflow3 : SequentialWorkflowActivity
{
public static readonly DependencyProperty MyIntProperty =
DependencyProperty.Register("MyInt", typeof(int), typeof(Workflow3));
public static readonly DependencyProperty MyStringProperty =
DependencyProperty.Register("MyString", typeof(string), typeof(Workflow3));
public Workflow3()
{
InitializeComponent();
this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode);
}
public int MyInt
{
get { return (int)GetValue(MyIntProperty); }
set { SetValue(MyIntProperty, value); }
}
public string MyString
{
get { return (string)GetValue(MyStringProperty); }
set { SetValue(MyStringProperty, value); }
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
Console.WriteLine("Invoke WF: Int = {0}, String = {1}", this.MyInt, this.MyString);
}
}
Workflow2 是仅包含 ReplicatorActivity 的托管工作流。ReplicatorActivity 仅包含一个 InvokeWorkflowActivity,它的 TargetWorkflow 设置为 Workflow3。代码隐藏如下:
public sealed partial class Workflow2 : SequentialWorkflowActivity
{
// Variables used in bindings
public int InvokeWorkflowActivity1_MyInt = default(int);
public string InvokeWorkflowActivity1_MyString = string.Empty;
public Workflow2()
{
InitializeComponent();
// Bind MyInt parameter of target workflow to my InvokeWorkflowActivity1_MyInt
WorkflowParameterBinding wpb1 = new WorkflowParameterBinding("MyInt");
wpb1.SetBinding(WorkflowParameterBinding.ValueProperty, new ActivityBind(this.GetType().Name, "InvokeWorkflowActivity1_MyInt"));
this.invokeWorkflowActivity1.ParameterBindings.Add(wpb1);
// Bind MyString parameter of target workflow to my InvokeWorkflowActivity1_MyString
WorkflowParameterBinding wpb2 = new WorkflowParameterBinding("MyString");
wpb2.SetBinding(WorkflowParameterBinding.ValueProperty, new ActivityBind(this.GetType().Name, "InvokeWorkflowActivity1_MyString"));
this.invokeWorkflowActivity1.ParameterBindings.Add(wpb2);
// Add event handler for Replicator's Initialized event
this.replicatorActivity1.Initialized += new EventHandler(ReplicatorInitialized);
// Add event handler for Replicator's ChildInitialized event
this.replicatorActivity1.ChildInitialized += new EventHandler<ReplicatorChildEventArgs>(this.ChildInitialized);
}
private void ReplicatorInitialized(object sender, EventArgs e)
{
// Find how many workflows I want
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass() { MyInt = 1, MyString = "Str1" });
list.Add(new MyClass() { MyInt = 2, MyString = "Str2" });
list.Add(new MyClass() { MyInt = 3, MyString = "Str3" });
// Assign list to replicator
replicatorActivity1.InitialChildData = list;
}
private void ChildInitialized(object sender, ReplicatorChildEventArgs e)
{
// This is the activity that is initialized
InvokeWorkflowActivity currentActivity = (InvokeWorkflowActivity)e.Activity;
// This is the initial data
MyClass initialData = (MyClass)e.InstanceData;
// Setting the initial data to the activity
InvokeWorkflowActivity1_MyInt = initialData.MyInt;
InvokeWorkflowActivity1_MyString = initialData.MyString;
}
public class MyClass
{
public int MyInt { get; set; }
public string MyString { get; set; }
}
}
预期结果如下:
Invoke WF: Int = 1, String = Str1
Invoke WF: Int = 2, String = Str2
Invoke WF: Int = 3, String = Str3
希望这会对您有所帮助。
我意识到这篇文章已经过时了,但对于那些在谷歌上找到同样问题的人来说,这就是你需要做的:
ReplicatorActivity 公开了一个名为 ChildInitialized 的事件处理程序。为此事件创建您的处理程序,您将在其中收到一个 ReplicatorChildEventArgs。在其中,您可以通过事件参数接收活动,如下所示:
InvokerActivity activity = (e.Activity as InvokerActivity);
if (activity != null)
{
activity.MyParam = e.InstanceData as MyParamType;
}
现在,当您运行它时,ReplicatorActivity 将为集合中的每个项目调用一次此方法,并将传递它将产生的每个 InvokerActivities 的参数。
e.InstanceData 将是 Replicator 正在迭代的集合中的下一个对象。
您可以像这样在目标工作流中声明两个属性:
public static readonly DependencyProperty MyIntProperty =
DependencyProperty.Register("MyInt", typeof(int), typeof(Workflow3));
public static readonly DependencyProperty MyStringProperty =
DependencyProperty.Register("MyString", typeof(string), typeof(Workflow3));
public int MyInt
{
get { return (int)GetValue(MyIntProperty); }
set { SetValue(MyIntProperty, value); }
}
public string MyString
{
get { return (string)GetValue(MyStringProperty); }
set { SetValue(MyStringProperty, value); }
}
之后,如果您检查“属性”选项卡,InvokeWorkflowActivity
您将看到该Parameters
类别中的两个属性。
您可以提供常量值,也可以将它们绑定到托管工作流的任何属性。