以下是我实现这一目标的方法:
1) 创建一个名为 WindowsFormsApplication7 的 Windows 窗体应用程序,使用最新的框架。
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 Activity,如 Class1 中所述(公共类 UpdateLabel : CodeActivity)必须存在于工具箱中
8) 从工具箱中,将 UpdateLabel 和 WriteLine 活动拖入 Activity1
9) 将以下参数 lbl(标签)和 txt(字符串)添加到 Activity1
10) 在 UpdateLabel 活动中单击一次,按 F4(属性)并更新活动的参数,如图所示
11) 按 F5 编译并运行应用程序。在文本框中插入一些文本并按下按钮。文本必须显示在标签中,由 Activity1 更新,并在输出窗口中,由 WriteLine 活动更新
12) 恭喜!!!