以编程方式在流程图工作流程中创建分配
我需要以编程方式定义一个可序列化的流程图 Windows 工作流,它接受输入参数并返回结果。我了解如何使用设计器创建这些工作流程,但我需要在代码中执行此操作并使流程图工作流程可序列化(因此没有 lambda 表达式)。
我无法获取分配的“收件人”字段。下面的代码创建了一个 WriteLine 和后面的分配的流程图工作流程。
var ab = new ActivityBuilder<string> {
Name = "ActivityBuilt",
Implementation = new Flowchart {
StartNode = new FlowStep {
Action = new WriteLine { Text = new VisualBasicValue<string>("greeting") },
Next = new FlowStep {
Action = new Assign {
DisplayName = "Set result",
To = new OutArgument<string>(new VisualBasicReference<string> {
ExpressionText = "results"}),
Value = new VisualBasicValue<string>("bye")
}
}
}
}
};
ab.Properties.Add(new DynamicActivityProperty {
Name = "greeting",
Type = typeof (InArgument<string>),
Value = "hello"});
ab.Properties.Add(new DynamicActivityProperty {
Name = "results",
Type = typeof (OutArgument<string>),
Value = "bye"});
// Convert the ActivityBuilder to a callable activity
using (var sw = new StringWriter()) {
using (var xw = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(sw, new XamlSchemaContext()))) {
XamlServices.Save(xw, LastCreated);
}
using (var sr = new StringReader(sw.ToString())) {
var wf = ActivityXamlServices.Load(sr);
return wf;
}
}
当我尝试从 ActivityBuilder 转换为 Activity 时,上面的代码失败,并显示“无法从文本“再见”创建“OutArgument”。”如果我不使用 OutArgument 并只是将内容传入,那么效果很好。
我的问题是要在 To 属性中放入什么?如何引用添加到 ActivityBuilder.Properties 的 OutArgument? VisualBasicReference 不是 OutArgument。我是否让事情变得比需要的更加困难?
感谢您的任何提示!
编辑:我想以编程方式创建工作流程。工作流需要有输入参数和返回结果(输出参数)。
我了解如何创建工作流程以及如何声明和使用输入参数。我使用 ActivityBuilder 创建工作流并通过 ActivityBuilder 的属性设置 InArgument。我通过序列化到 XAML,然后使用 ActivityXamlServices.Load 反序列化,从 ActivityBuilder 创建工作流。
我不明白的是如何从工作流程中获得结果。我认为它涉及 OutArgument。如何/在哪里将 OutArgument 添加到工作流程?我认为我提供的代码片段会将值分配给 OutArgument,但是对 ActivityXamlServices.Load 的调用失败,并抱怨无法创建 OutArgument。
- 使用 ActivityBuilder 的方法是否正确?
- 分配操作的“收件人”字段是否正确引用了 OutArgument?
- 如何使 ActivityBuilder 了解 OutArgument 并且仍然能够转换为活动/工作流程?
希望这能澄清我的问题。
I need to programmatically define a serializable flowchart Windows Workflow that accepts input arguments and returns a result. I understand how to create these workflows using a designer, but I need to do it in code and have the flowchart workflow be serializable (so no lambda expressions).
I'm having trouble getting the "To" field of the Assign. The code below creates a flowchart workflow of a WriteLine followed by an Assign.
var ab = new ActivityBuilder<string> {
Name = "ActivityBuilt",
Implementation = new Flowchart {
StartNode = new FlowStep {
Action = new WriteLine { Text = new VisualBasicValue<string>("greeting") },
Next = new FlowStep {
Action = new Assign {
DisplayName = "Set result",
To = new OutArgument<string>(new VisualBasicReference<string> {
ExpressionText = "results"}),
Value = new VisualBasicValue<string>("bye")
}
}
}
}
};
ab.Properties.Add(new DynamicActivityProperty {
Name = "greeting",
Type = typeof (InArgument<string>),
Value = "hello"});
ab.Properties.Add(new DynamicActivityProperty {
Name = "results",
Type = typeof (OutArgument<string>),
Value = "bye"});
// Convert the ActivityBuilder to a callable activity
using (var sw = new StringWriter()) {
using (var xw = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(sw, new XamlSchemaContext()))) {
XamlServices.Save(xw, LastCreated);
}
using (var sr = new StringReader(sw.ToString())) {
var wf = ActivityXamlServices.Load(sr);
return wf;
}
}
The above code fails when I try to convert from ActivityBuilder to Activity saying "Failed to create a 'OutArgument' from the text 'bye'." This works fine if I don't use the OutArgument and just pass things in.
My question is what to put in the To property? How do I reference the OutArgument I add to the ActivityBuilder.Properties? A VisualBasicReference isn't an OutArgument. Am I making this more difficult than it needs to be?
Thanks for any hints!
Edit: I want to create a workflow programmatically. The workflow needs to have input arguments and return results (output arguments).
I understand how to create the workflow and how to declare and use input arguments. I'm using an ActivityBuilder to create the workflow and to set the InArgument via the ActivityBuilder's properties. I create the workflow from the ActivityBuilder by serializing to XAML and then deserializing using ActivityXamlServices.Load.
What I don't understand is how to get a result from the workflow. I assume it involves an OutArgument. How/where do I add an OutArgument to the workflow? I thought the code snippet I gave would assign a value to an OutArgument, but the call to ActivityXamlServices.Load fails with a complaint that it is unable to create the OutArgument.
- Is the approach of using ActivityBuilder correct?
- Is the "To" field of the Assign action properly referencing an OutArgument?
- How do I make the ActivityBuilder aware of the OutArgument and still be able to convert to an Activity / workflow?
Hope this clarifies my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码至少存在 3 个问题:
尝试以下代码:
There are atleast 3 problems with the code:
Try the following code: