WF4.0 beta2提供了Pick活动用于完成基于事件的控制流。 该活动可以有多个PickBranch分支。每个分支有Trigger和Action两部分。当Trigger 被触发时,会执行Action中的Activity。Pick活动只要有一个PickBranch的Trigger被触发,其他PickBranch就不会被触发了 。
1.举例说明:有两个分支,我们等待用户输入过期就结束,工作如下图:
2.工作流对应的XAML如下:
<Activity mc:Ignorable="sap" x:Class="........> <Sequence sad:XamlDebuggerXmlReader.FileName="......\Sequence1.xaml" sap:VirtualizedContainerService.HintSize="656,462"> <Sequence.Variables> <Variable x:TypeArguments="x:String" Name="name" /> </Sequence.Variables> <sap:WorkflowViewStateService.ViewState> <scg:Dictionary x:TypeArguments="x:String, x:Object"> <x:Boolean x:Key="IsExpanded">True</x:Boolean> </scg:Dictionary> </sap:WorkflowViewStateService.ViewState> <Pick sap:VirtualizedContainerService.HintSize="634,338"> <PickBranch sap:VirtualizedContainerService.HintSize="240,292"> <PickBranch.Trigger> <local:ReadString BookmarkName="["UserName"]" DisplayName="读取输入" sap:VirtualizedContainerService
.HintSize="210,100" Result="[name]" /> </PickBranch.Trigger> <WriteLine DisplayName="显示欢迎信息" sap:VirtualizedContainerService.HintSize="210,100" Text="["你好:
o " & name]" /> </PickBranch> <PickBranch sap:VirtualizedContainerService.HintSize="240,292"> <PickBranch.Trigger> <Delay DisplayName="设置过期时间" Duration="[System.TimeSpan.FromSeconds(5)]" sap:VirtualizedContainerService
.HintSize="210,100" /> </PickBranch.Trigger> <WriteLine DisplayName="时间过期提示" sap:VirtualizedContainerService.HintSize="210,100" Text="时间过期" /> </PickBranch> </Pick> </Sequence> </Activity>
3.上面是可视化的方式设计工作流,还可以使用代码方式,如下:
static Activity CreateWF() { Variable<string> name = new Variable<string>(); // Body Sequence body = new Sequence() { Variables = { name }, Activities = { new Pick { Branches = { new PickBranch { Trigger = new ReadString { Result = name, BookmarkName = bookmarkName }, Action = new WriteLine { Text = new InArgument<string>(env => "你好:" + name.Get(env)) } }, new PickBranch { Trigger = new Delay { Duration = TimeSpan.FromSeconds(5) }, Action = new WriteLine { Text = "时间过期" } } } } } }; return body; }
public sealed class ReadString : NativeActivity<string> { [RequiredArgument] public InArgument<string> BookmarkName { get; set; } protected override bool CanInduceIdle { get { return true; } } protected override void Execute(NativeActivityContext context) { context.CreateBookmark(this.BookmarkName.Get(context), new BookmarkCallback(OnReadComplete)); } void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state) { string input = state as string; context.SetValue(this.Result, input); } }
5.宿主程序如下
static string bookmarkName = "UserName"; public static void Main(string[] args) { ManualResetEvent completedEvent = new ManualResetEvent(false); AutoResetEvent idleEvent = new AutoResetEvent(false); //WorkflowApplication application = new WorkflowApplication(new Sequence1()); WorkflowApplication application = new WorkflowApplication(CreateWF()); application.Idle += delegate(WorkflowApplicationIdleEventArgs e) { idleEvent.Set(); }; application.Completed += delegate(WorkflowApplicationCompletedEventArgs e) { completedEvent.Set(); }; application.Run(); idleEvent.WaitOne(); Console.WriteLine("你的名字时什么(5秒)"); string text = Console.ReadLine();
application.ResumeBookmark(bookmarkName, text); completedEvent.WaitOne(); Console.WriteLine("工作流执行完成"); Console.ReadLine(); }
作者:生鱼片
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。