這裏如下我實現這一目標的方法:
1)創建一個Windows窗體稱爲WindowsFormsApplication7應用程序,使用最新的框架。
2)確保包括所有引用

3)添加班級用下面的代碼。在形式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Timers;
using System.Reflection;
using System.Activities;
using System.Activities.Statements;
namespace WindowsFormsApplication7
{
public class UpdateLabel : CodeActivity
{
Action y;
public InArgument<Label> lbl { get; set; }
public InArgument<string> text { get; set; }
protected override void Execute(CodeActivityContext context)
{
((Label)context.GetValue(lbl)).Invoke(y =() => ((Label)context.GetValue(lbl)).Text = context.GetValue(text).ToString());
}
}
}
4)雙擊和與這一個替換的代碼。不要介意錯誤。它們將消失。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Timers;
using System.Reflection;
using System.Activities;
using System.Activities.Statements;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
Action y;
WorkflowApplication HomeCycleWFApp = null;
AutoResetEvent HomeEvent = null;
Dictionary<string, object> inArgs = new Dictionary<string, object>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
RunHomeCycle(label1, textBox1.Text);
}
public void RunHomeCycle(Label lbl, string txt)
{
button1.Enabled = false;
if (!inArgs.ContainsKey("lbl"))
{
inArgs.Add("lbl", lbl);
}
if (!inArgs.ContainsKey("txt"))
{
inArgs.Add("txt", txt);
}
else
{
inArgs["txt"] = txt;
}
HomeEvent = new AutoResetEvent(false);
HomeCycleWFApp = new WorkflowApplication(new Activity1(), inArgs);
HomeCycleWFApp.Completed = delegate (WorkflowApplicationCompletedEventArgs e)
{
button1.Invoke(y =() => button1.Enabled = true);
HomeEvent.Set();
};
HomeCycleWFApp.Run();
}
}
}
5)將以下控件添加到窗體
LABEL1,TextBox1中和button1的

6)添加稱爲Activity1.xaml

的工作流活動
7)編譯解決方案(F6)。的UpdateLabel活動,如在Class1的(公共類UpdateLabel:CodeActivity)描述必須存在於工具箱
8)從工具箱,將UpdateLabel和的WriteLine活動納入活性1

9)以下參數LBL(標籤),TXT(串)添加到活動1
10)在UpdateLabel活動,按F4(屬性)點擊一次,更新活動的參數如圖所示

11)按F5編譯並運行應用程序。在文本框中插入一些文本,然後按按鈕。文本必須顯示在標籤中,由Activity1更新,並在輸出窗口中由WriteLine活動更新
12)恭喜!!!
你到目前爲止編碼了什麼...? – MethodMan 2011-12-15 17:17:56
+1好問題,wf4需要更多的愛。然而,sl msdn vidz很好。 – 2012-07-25 00:44:15