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

WF4.0 Beta2:关于动态保存和装载XAML工作流

Posted on 2009-10-24 12:33  生鱼片  阅读(2767)  评论(5编辑  收藏  举报

1.ActivityXamlServices类的主要功能是从XAML文件创建活动树的实例。提供了下面四个方法:

CreateBuilderReader:Overloaded. Maps an x:Class activity tree to an ActivityBuilder or ActivityBuilder<(Of <(TResult>)>).
CreateBuilderWriter:Maps an ActivityBuilder or ActivityBuilder<(Of <(TResult>)>) from the specified writer to an
x:Class activity tree.
CreateReader:Overloaded. Maps an x:Class activity tree to an DynamicActivity or DynamicActivity<(Of <(TResult>)>).
Load:Overloaded. Creates an instance of a declarative workflow.
Load方法遇到标记有<Activity x:Class的XAML会返回一个DynamicActivity. 
 
 2.下面一个具体的例子:
ActivityBuilder ab1 = new ActivityBuilder();
 ab1.Name = "HelloWorldCary";
 ab1.Properties.Add(new DynamicActivityProperty { Name = "input1", Type = typeof(InArgument<string>) });
 ab1.Properties.Add(new DynamicActivityProperty { Name = "input2", Type = typeof(InArgument<string>) });
 ab1.Properties.Add(new DynamicActivityProperty { Name = "output", Type = typeof(OutArgument<string>) });
 ab1.Implementation = new Sequence
 {
     Activities = 
     {
         new WriteLine{Text="Getting Started"},
         new Delay{Duration=TimeSpan.FromSeconds(4)},
         new WriteLine{Text=new VisualBasicValue<string>{ExpressionText="input1+input2"}},
         new Assign<string>{To=new VisualBasicReference<string>{ExpressionText="output"},
                 Value=new VisualBasicValue<string>{ExpressionText="input1+input2+\"  that's ok!\""}}
     }
 };
 StringBuilder sb = new StringBuilder();
 StringWriter tw = new StringWriter(sb);
 XamlWriter xw = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(tw,new XamlSchemaContext()));
 XamlServices.Save(xw,ab1);
 string serializedAB = sb.ToString();

 DynamicActivity da2 = ActivityXamlServices.Load(new StringReader(serializedAB)) as DynamicActivity;
 var result = WorkflowInvoker.Invoke(da2, new Dictionary<string, object> { {"input1","hello"},{"input2","world"}});
 Console.WriteLine("result text is {0}",result["output"]);

 ActivityBuilder ab = XamlServices.Load(
     ActivityXamlServices.CreateBuilderReader(
         new XamlXmlReader(new StringReader(serializedAB)))) as ActivityBuilder;
 Console.WriteLine("there are {0} arguments in the activity builder",ab.Properties.Count);
 Console.WriteLine("press enter to exit");
 Console.WriteLine(sb.ToString());
 Console.ReadLine();

 

3.执行的结果如下:
--------------------------------------------------------------------------------------------
Getting Started
helloworld
result text is helloworld  that's ok!
there are 3 arguments in the activity builder
press enter to exit
--------------------------------------------------------------------------------------------

4.生成的XAML如下:

<?xml version="1.0" encoding="utf-16"?>
<Activity x:Class="HelloWorldCary" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <x:Members>
        <x:Property Name="input1" Type="InArgument(x:String)" />
        <x:Property Name="input2" Type="InArgument(x:String)" />
        <x:Property Name="output" Type="OutArgument(x:String)" />
    </x:Members>
    <Sequence>
        <WriteLine Text="Getting Started" />
        <Delay Duration="00:00:04" />
        <WriteLine Text="[input1+input2]" />
        <Assign x:TypeArguments="x:String" To="[output]" Value="[input1+input2+&quot;  that's ok!&quot;]" />
    </Sequence>
</Activity>