- Microsoft Windows Workflow Foundation 4.0 Cookbook
- Andrew Zhu
- 311字
- 2021-04-13 17:00:36
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.
- Create a workflow project:
Create a new Workflow Console Application under the
Chapter01
solution and name the projectConvertWFInstanceToXML
. Delete theWorkflow1.xaml
file that is created by default. - 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()); } } }
- 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:
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>
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.
- GIMP 2.6 cookbook
- Excel商務數據分析與應用(慕課版)
- 中文版CorelDRAW X7基礎培訓教程(移動學習版)
- CINEMA 4D R20完全實戰技術手冊
- 3dsMax 2018動畫制作基礎教程(第4版)
- Learning the Yahoo! User Interface library
- Altium Designer 21 PCB設計官方指南(高級實戰)
- 中文版Photoshop 2023從入門到精通
- AI寫實人物繪畫關鍵詞圖鑒(Stable Diffusion版)
- 攝影師的后期課:Lightroom后期技法篇
- 設計必修課:Axure RP 9互聯網產品原型設計
- Photoshop圖像處理與制作
- Premiere短視頻制作(第2版·全彩慕課版)
- Photoshop CC 2015課堂實錄
- Mastercam軟件應用技術基礎(X2版)