【问题标题】:Simple app using Windows Workflow and winforms NOT console使用 Windows Workflow 和 winforms NOT 控制台的简单应用程序
【发布时间】:2011-12-15 16:52:39
【问题描述】:

我正在寻找一个简单的入门应用程序,它允许您输入一个 1 - 10 的值,这个值被传递给一个 WF 规则,该规则评估它是否大于、小于或等于 5 并将结果返回给在标签中显示结果的 windows 窗体应用程序。

我可以找到很多 .net 3.5 控制台应用程序教程,但没有任何内容显示如何使用 Windows 窗体和 .net 4 传入和接收结果!

它不需要是上面的示例,但它需要向我展示如何将值传递到规则中,编写规则并从 .net 4 c# 中的 windows 窗体应用程序中读取规则的结果。

我迷路了!

我的基本代码现在可以运行,以防它帮助其他人:

var workflow = new Activity1();

        IDictionary<string, object> inputs = new Dictionary<string, object>();
        inputs["firstname"] = textBox1.Text;
        IDictionary<string, object> outputs = WorkflowInvoker.Invoke(workflow, inputs);
        textBox2.Text= outputs["greeting"].ToString();

firstname 是传递给工作流的带有方向的参数。 greeting 是在工作流程中分配了方向的参数。

【问题讨论】:

  • 到目前为止你编码了什么......?
  • +1 好问题,wf4 需要更多的爱。 sl msdn vidz 但是很好。

标签: c# winforms .net-4.0 workflow-foundation


【解决方案1】:

以下是我实现这一目标的方法:
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) 恭喜!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多