随笔 - 234, 文章 - 12, 评论 - 1671, 阅读 - 74万
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on   生鱼片  阅读(2768)  评论(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>

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2008-10-24 隐藏自定义复合活动的内部实现
点击右上角即可分享
微信分享提示