Converting a WF program instance to XAML
In real applications, we would like to write and test WF programs in imperative code, while storing, running, and transmitting Workflow as an XAML string or file. In this task, we will convert a WF program instance to an XAML string.
How to do it...
using System;
using System.Activities;
using System.Activities.Statements;
using System.Text;
using System.Xaml;
using System.Activities.XamlIntegration;
using System.IO;
namespace ConvertWFObjectToXML
{
class Program
static void Main(string[] args)
//Create a Workflow instance object
ActivityBuilder ab = new ActivityBuilder();
ab.Implementation = new Sequence()
Activities =
new WriteLine{Text="Message from Workflow"}
}
};
//Convert Workflow instance to xml string
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XamlWriter xw =
ActivityXamlServices.CreateBuilderWriter(
new XamlXmlWriter(sw,
new XamlSchemaContext()));
XamlServices.Save(xw, ab);
Console.WriteLine(sb.ToString());
How it works...
Consider the following code line:
XamlServices provides services for the common XAML tasks of reading XAML and writing an object graph, or reading an object and writing out an XAML file. This statement reads an ActivityBuilder object and writes XAML to an XamlWriter object.
We use ActivityBuilder as an activity wrapper so that the output XAML is a loadable Workflow. In other words, if we save, say, a Sequence activity to an XamlWriter directly, then the output XML Workflow will be unloadable for further use.