博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

WF4.0 Beta2:Pick Activity的使用

Posted on 2009-11-04 21:58  生鱼片  阅读(2699)  评论(4编辑  收藏  举报

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