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+" that's ok!"]" /> </Sequence> </Activity>
本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/10/24/1589076.html,如需转载请自行联系原作者