WF4.0 Beta2:Pick Activity的使用

WF4.0 beta2提供了Pick活动用于完成基于事件的控制流。 该活动可以有多个PickBranch分支。每个分支有Trigger和Action两部分。当Trigger 被触发时,会执行Action中的Activity。Pick活动只要有一个PickBranch的Trigger被触发,其他PickBranch就不会被触发了 。

1.举例说明:有两个分支,我们等待用户输入过期就结束,工作如下图:

clip_image002

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="[&quot;UserName&quot;]" DisplayName="读取输入" sap:VirtualizedContainerService
.HintSize
="210,100" Result="[name]" /> </PickBranch.Trigger> <WriteLine DisplayName="显示欢迎信息" sap:VirtualizedContainerService.HintSize="210,100" Text="[&quot;你好:
o &quot; &amp; 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;
        }

4.我们需要准备一个读取输入的活动,代码如下:

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(); }

本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/11/04/WFBETA2-Pick.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值