《WF编程》系列之12 - 编译工作流:XAML序列化
《WF编程》系列之12 - 编译工作流:XAML序列化
2.3.2.4 XAML序列化
我们可以从工作流实例中生成XAML,因为XAML本质上就是托管对象的XML序列化.下面的代码将输出正在运行的工作流的标记:
using (WorkflowRuntime runtime = new WorkflowRuntime())
using (StringWriter stream = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(stream))
{
Type t = typeof(MyWorkflow);
WorkflowInstance instance = runtime.CreateWorkflow(t);
WorkflowMarkupSerializer serializer;
serializer = new WorkflowMarkupSerializer();
serializer.Serialize(
writer,
instance.GetWorkflowDefinition()
);
Console.WriteLine(stream.ToString());
}
using (StringWriter stream = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(stream))
{
Type t = typeof(MyWorkflow);
WorkflowInstance instance = runtime.CreateWorkflow(t);
WorkflowMarkupSerializer serializer;
serializer = new WorkflowMarkupSerializer();
serializer.Serialize(
writer,
instance.GetWorkflowDefinition()
);
Console.WriteLine(stream.ToString());
}
如果在之前包含自定义活动的工作流上运行这段代码,我们将获得下面的XAML.
<?xml version="1.0" encoding="utf-8"?>
<ns0:MyWorkflow x:Name="MyWorkflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ns0="clr-namespace:ReSerialize;Assembly=chapter2_Host"/>
<ns0:MyWorkflow x:Name="MyWorkflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ns0="clr-namespace:ReSerialize;Assembly=chapter2_Host"/>
需要注意的是,工作流定义现在还并不是透明可见的.当工作流编译器从工作流定义中创建了一个新的类型时,工作流定义变得固定且看不到内部活动顺序.但我们通过使用XAML激活机制仍然可以创建上面工作流的实例,激活机制将在下一节详述.XAML激活驱动XML并从标记指令中创建对象.当激活初始化MyWorkflow后,InitializeComponent方法将创建工作流中的所有活动,所以序列化不需要从XAML中获得显式的子活动列表.实例会像其它范例一样工作,并且会执行WriteLineActivity.
http://xiaoshatian.cnblogs.com/