官术网_书友最值得收藏!

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...

  1. Create a workflow project:

    Create a new Workflow Console Application under the Chapter01 solution and name the project ConvertWFInstanceToXML. Delete the Workflow1.xaml file that is created by default.

  2. Write code to create the workflow and its host:

    Open Program.cs file and change the code as follows:

    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());
            }
        }
    }
  3. Run it:

    Set ConvertWFInstanceToXML as Startup project. Press Ctrl+F5 to build and run the workflow without debugging. The application should run in a console window and print the message as shown in the following screenshot:

    How to do it...

Consider the following XML string reformatted from the screenshot:

<?xml version="1.0" encoding="utf-16"?>
<Activity  x:Class="{x:Null}" 
                  xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" 
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
       <Sequence>
            <WriteLine Text="Hello" />
       </Sequence>
</Activity>

How it works...

Consider the following code line:

XamlServices.Save(xw, ab);

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.

主站蜘蛛池模板: 怀化市| 郓城县| 赤城县| 华容县| 新化县| 昔阳县| 延吉市| 宣恩县| 南丹县| 鹰潭市| 东兴市| 湖口县| 宁城县| 垣曲县| 资兴市| 南昌市| 读书| 济宁市| 景宁| 高唐县| 锡林浩特市| 色达县| 芦溪县| 乐平市| 古蔺县| 商南县| 治县。| 上思县| 苗栗县| 班戈县| 邵武市| 柳江县| 西畴县| 沈阳市| 黑山县| 江川县| 东乡族自治县| 松江区| 丽水市| 宁海县| 泰宁县|