Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Windows工作流和winforms而不是控制台的简单应用程序_C#_Winforms_.net 4.0_Workflow Foundation - Fatal编程技术网

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

C# 使用Windows工作流和winforms而不是控制台的简单应用程序,c#,winforms,.net-4.0,workflow-foundation,C#,Winforms,.net 4.0,Workflow Foundation,我正在寻找一个简单的入门应用程序,它允许您键入一个值1-10。该值将传递给WF规则,该规则将评估该值是否大于、小于或等于5,并将结果返回到windows窗体应用程序,该应用程序将结果显示在标签中 我可以找到很多.net 3.5控制台应用程序教程,但没有任何内容显示如何使用windows窗体和.net 4传递并接收结果 它不需要是上面的示例,但需要向我展示如何将值传递到规则中,编写规则并从.net 4 c#中的windows窗体应用程序中读取规则的结果 我迷路了 我的基本代码现在正在运行,以防它帮

我正在寻找一个简单的入门应用程序,它允许您键入一个值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();
var workflow=new Activity1();
IDictionary inputs=新字典();
输入[“firstname”]=textBox1.Text;
IDictionary outputs=WorkflowInvoker.Invoke(工作流,输入);
textBox2.Text=输出[“问候语”].ToString();
firstname是一个向工作流传递方向的参数。
问候语是一种在工作流程中指定了方向的论点。

这里遵循我达到这个目标的方法:
1) 使用最新的框架创建名为WindowsFormsApplication7的Windows窗体应用程序。
2) 确保包括所有参考资料


3)使用以下代码添加一个类


使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统诊断;
使用系统线程;
使用System.IO;
使用系统计时器;
运用系统反思;
使用系统活动;
使用系统、活动、报表;
命名空间Windows窗体应用程序7
{
公共类UpdateLabel:CodeActivity
{
行动y;
公共非语法lbl{get;set;}
公共非语法文本{get;set;}
受保护的覆盖无效执行(CodeActivityContext上下文)
{
((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();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统诊断;
使用系统线程;
使用System.IO;
使用系统计时器;
运用系统反思;
使用系统活动;
使用系统、活动、报表;
命名空间Windows窗体应用程序7
{
公共部分类Form1:Form
{
行动y;
WorkflowApplication HomeCycleWFApp=null;
AutoResetEvent HomeEvent=null;
字典inArgs=新字典();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
标签1.Text=“”;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
RunHomeCycle(label1,textBox1.Text);
}       
公共无效RunHomeCycle(标签lbl,字符串txt)
{
按钮1.启用=错误;
如果(!inArgs.ContainsKey(“lbl”))
{
增加(“lbl”,lbl);
}
如果(!inArgs.ContainsKey(“txt”))
{
添加(“txt”,txt);
}
其他的
{
inArgs[“txt”]=txt;
}
HomeEvent=新的自动重置事件(false);
HomeCycleWFApp=新的工作流应用程序(新活动1(),inArgs);
HomeCycleWFApp.Completed=委托(WorkflowApplicationCompletedEventArgs e)
{
按钮1.Invoke(y=()=>button1.Enabled=true);
homevent.Set();
};
HomeCycleWFApp.Run();
}
}
}

5)将以下控件添加到表单中
标签1、文本框1和按钮1


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


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

8) 从工具箱中,将UpdateLabel和WriteLine活动拖动到Activity1中


9) 将以下参数lbl(标签)和txt(字符串)添加到Activity1


10)在UpdateLabel活动中单击一次,按F4(属性)并更新活动的参数,如图所示


11)按F5编译并运行应用程序。在文本框中插入一些文本并按下按钮。文本必须显示在标签中,由Activity1更新,并显示在输出窗口中,由WriteLine活动更新


12) 恭喜你

以下是我实现这一目标的方法:
1) 使用最新的框架创建名为WindowsFormsApplication7的Windows窗体应用程序。
2) 确保包括所有参考资料


3)使用以下代码添加一个类


使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统诊断;
使用系统线程;
使用System.IO;
使用系统计时器;
运用系统反思;
使用系统活动;
使用系统、活动、报表;
命名空间Windows窗体应用程序7
{
公共类UpdateLabel:CodeActivity
{
行动y;
公共非语法lbl{get;set;}
公共非语法文本{get;set;}
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();
        }

    }
}