使用Windows工作流和winforms而不是控制台的简单应用程序

发布于 2024-12-21 07:59:58 字数 701 浏览 3 评论 0原文

我正在寻找一个简单的入门应用程序,允许您输入值 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();

名字是一个参数,其方向传递给工作流程。 问候语是一个在工作流程中分配方向的参数。

I'm looking for a simple starter app that allows you to type in a value 1 - 10 this value is passed to a WF rule that evaluates if it is greater, less than or equal to 5 and returns the results to the windows forms app which displays the results in a label.

I can find a lot of .net 3.5 console app tutorials but nothing that shows how to pass into and receive the result back using windows forms and .net 4!

it doesn't need to be the above example but it needs to show me how to pass a value into the rule, write the rule and read the result from the rule from within a windows forms app in .net 4 c#.

I'm lost!

My Basic code now working in case it helps others:

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 is an argument with direction in passed to the work flow.
greeting is an argument with direction out assigned within the work flow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2024-12-28 07:59:58

以下是我实现这一目标的方法:
1)创建一个名为WindowsFormsApplication7的Windows窗体应用程序,使用最新的框架。
创建像平常一样的 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) 将以下控件添加到表单
标签 1、文本框 1 和按钮 1
输入图片此处描述

6) 添加名为 Activity1.xaml 的工作流活动

在此处输入图像描述

7) 编译解决方案 (F6)。 UpdateLabel 活动,如 Class1(公共类 UpdateLabel:CodeActivity)中所述,必须存在于工具箱中

8) 从工具箱中,将 UpdateLabel 和 WriteLine 活动拖到 Activity1
输入图片此处描述
在此处输入图像描述

9) 将以下参数 lbl(标签)和 txt(字符串)添加到 Activity1
输入图片此处描述

10) 在 UpdateLabel 活动中单击一次,按 F4(属性)并更新活动的参数,如图所示
输入图片此处描述

11) 按 F5 编译并运行应用程序。在文本框中插入一些文本,然后按按钮。文本必须显示在标签中(由 Activity1 更新)和输出窗口中(由 WriteLine 活动更新)
输入图片此处描述

12)恭喜!!!

Here follows the approach I got to reach this goal:
1) Create a Windows Forms Application called WindowsFormsApplication7, Use the latest Framework.
Create a Windows Forms Application as usual

2) Make sure to include all references
enter image description here

3) Add a Class with the following code.

enter image description here

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) Double click in the form and replace the code with this one. Don't mind the errors. They will vanish.

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) Add the following controls to the form
label1, textbox1 and button1
enter image description here

6) Add an Workflow Activity called Activity1.xaml

enter image description here

7) Compile the solution (F6). The UpdateLabel Activity, as described in the Class1 (public class UpdateLabel : CodeActivity) must be present in the ToolBox

8) From the ToolBox, drag a UpdateLabel and a WriteLine activities into the Activity1
enter image description here
enter image description here

9) Add the following Arguments lbl (Label) and txt (string) to the Activity1
enter image description here

10) Click once in the UpdateLabel activity, press F4 (Properties) and update the parameters of the activity as shown
enter image description here

11) Press F5 to compile and run the application. Insert some text in the textbox and press the button. The text must be shown in the label, updated by the Activity1, and in the Output Window, updated by the WriteLine activity
enter image description here

12) Congrats!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文