示例来自microsoft windows workflow foundation 4.0 cookbook P31
1. Create a workfow project:
Create a new Workfow Console Application under the Chapter01 solution and
name the project as LoadUpWorkflowFromXML.
Create a new Workfow Console Application under the Chapter01 solution and
name the project as LoadUpWorkflowFromXML.
2. Author a workfow:
Author the Workflow1.xaml fle; this workfow will print a string to console as shown
in the following screenshot:
Author the Workflow1.xaml fle; this workfow will print a string to console as shown
in the following screenshot:
3. Create code to load up the workfow instance from an XAML string:
Open Program.cs fle and change code as follow:
using System;
using System.Activities;
using System.Activities.Statements;
using System.IO;
using System.Collections;
using System.Text;
using System.Activities.XamlIntegration;
namespace LoadUpWorkflowFromXML {
class Program {
static void Main(string[] args) {
string filePath= @"C:\WF4Cookbook\Chapter01\
LoadUpWFFromXML\Workflow1.xaml";
string tempString="";
StringBuilder xamlWFString = new StringBuilder();
StreamReader xamlStreamReader =
new StreamReader(filePath);
while (tempString != null){
tempString = xamlStreamReader.ReadLine();
if (tempString != null) {
xamlWFString.Append(tempString);
Open Program.cs fle and change code as follow:
using System;
using System.Activities;
using System.Activities.Statements;
using System.IO;
using System.Collections;
using System.Text;
using System.Activities.XamlIntegration;
namespace LoadUpWorkflowFromXML {
class Program {
static void Main(string[] args) {
string filePath= @"C:\WF4Cookbook\Chapter01\
LoadUpWFFromXML\Workflow1.xaml";
string tempString="";
StringBuilder xamlWFString = new StringBuilder();
StreamReader xamlStreamReader =
new StreamReader(filePath);
while (tempString != null){
tempString = xamlStreamReader.ReadLine();
if (tempString != null) {
xamlWFString.Append(tempString);
}
}
Activity wfInstance = ActivityXamlServices.Load(
new StringReader(xamlWFString.ToString()));
WorkflowInvoker.Invoke(wfInstance);
}
}
}
}
Activity wfInstance = ActivityXamlServices.Load(
new StringReader(xamlWFString.ToString()));
WorkflowInvoker.Invoke(wfInstance);
}
}
}