在复制器活动中调用Workflow活动

问题描述 投票:0回答:3

我在复制器活动中有一个invokeworkflow活动。我正在尝试调用的工作流需要传递2个参数,一个整数和一个字符串参数,这些参数应该通过复制器活动传递给工作流。关于如何做到这一点的任何想法?

谢谢。

workflow-foundation
3个回答
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

希望这会对你有所帮助。


1
投票

我发现这篇文章很老了,但是对于那些在Google上发现同样问题的人来说,这就是你需要做的:

  1. 在自定义Activity中包含对InvokeWorkflow的调用 - 这将简化映射参数。在此Activity中,为要传递给调用的工作流的每个属性创建DependencyProperty。我们现在称之为“InvokerActivity”。然后在InvokeWorkflowActivity中,将TargetWorkflow的属性映射到InvokerActivity上的依赖项属性,如上面的Panos所示。注意:为了使省略号显示对象必须是具体类型。如果您的对象是界面,您将无法映射它。工作流将不知道如何实例化接口对象。
  2. 使用设计器将InvokerActivity放在ReplicatorActivity中。
  3. ReplicatorActivity公开名为ChildInitialized的事件处理程序。为此事件创建处理程序,在其中您将收到ReplicatorChildEventArgs。在其中,您可以通过事件args接收活动: InvokerActivity activity = (e.Activity as InvokerActivity); if (activity != null) { activity.MyParam = e.InstanceData as MyParamType; }

现在,当您运行它时,ReplicatorActivity将为集合中的每个项调用此方法一次,并将传递它将生成的每个InvokerActivities的参数。

e.InstanceData将是Replicator迭代的集合中的下一个对象。


0
投票

您可以在目标工作流中声明两个属性,如下所示:

    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的Properties选项卡,您将在Parameters类别中看到两个属性。

您可以提供常量值,也可以将它们绑定到托管工作流的任何属性。

© www.soinside.com 2019 - 2024. All rights reserved.