在自定义的Windows窗体中运行工作流:(把工作流的代码放入到文本框中)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ Encoding utf8 = Encoding.UTF8;
byte[] xmalcontent = utf8.GetBytes(textBox1.Text);
MemoryStream xamlstream = new MemoryStream(xmalcontent);
Activity workflow =
ActivityXamlServices.Load(xamlstream);
WorkflowApplication instance =
new WorkflowApplication(workflow);
instance.Run();
}
}
}
自定义节点:
在解决方案中添加
变量:
为变量和参数赋值:
输入参数:
指定到工作流中:
Dictionary<string, object> dics =
new Dictionary<string, object>();
dics.Add("arg1", "a1");
dics.Add("arg2", "a2");
dics.Add("arg3", "a3"); WorkflowApplication instance =
new WorkflowApplication(new Workflow1(), dics);
instance.Run();
Console.ReadLine();
新建DataAccess
public sealed class DataAccess : CodeActivity
{
// 定义一个字符串类型的活动输入参数
public InArgument<string> Text { get; set; } // 如果活动返回值,则从 CodeActivity<TResult>
// 并从 Execute 方法返回该值。
protected override void Execute(CodeActivityContext context)
{
string workflowdata = context.GetValue(Text);
Console.WriteLine("Code Activity:" + workflowdata); }
}
对Text进行赋值
输出参数:在被调用方进行修改
DataAccess工作单元
public sealed class DataAccess : CodeActivity
{
// 定义一个字符串类型的活动输入参数
public OutArgument<string> Text { get; set; } // 如果活动返回值,则从 CodeActivity<TResult>
// 并从 Execute 方法返回该值。
protected override void Execute(CodeActivityContext context)
{
context.SetValue(Text, "CodeActivity Value");
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<string, object> dics =
new Dictionary<string, object>();
//输入参数
dics.Add("arg1", "a1");
dics.Add("arg2", "a2"); WorkflowApplication instance =
new WorkflowApplication(new Workflow1(), dics);
//当工作流完成
instance.Completed =
new Action<WorkflowApplicationCompletedEventArgs>(workflowcompleted);
//运行工作流
instance.Run();
Console.ReadLine(); }
/// <summary>
/// 得到arg3
/// </summary>
/// <param name="args"></param>
static void workflowcompleted(WorkflowApplicationCompletedEventArgs args)
{
Console.WriteLine(args.Outputs["arg3"].ToString());
}
}
既能输入又能输出:
添加代码活动DataAccess:
public sealed class DataAccess : CodeActivity
{
// 定义一个字符串类型的活动输入参数
public InOutArgument<string> Text { get; set; } // 如果活动返回值,则从 CodeActivity<TResult>
// 并从 Execute 方法返回该值。
protected override void Execute(CodeActivityContext context)
{
string text = context.GetValue(this.Text);
Console.WriteLine("Code Activity In:" + text);
context.SetValue(Text, text.ToUpper());
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<string, object> dics =
new Dictionary<string, object>();
dics.Add("arg1", "a1");
dics.Add("arg2", "a2");
dics.Add("arg3", "a3"); WorkflowApplication instance =
new WorkflowApplication(new Workflow1(), dics);
instance.Completed =
new Action<WorkflowApplicationCompletedEventArgs>(workflowcompleted);
instance.Run();
Console.ReadLine();
} static void workflowcompleted(WorkflowApplicationCompletedEventArgs args)
{
Console.WriteLine(args.Outputs["arg3"].ToString());
}
}