Simple app using Windows Workflow and winforms NOT console

北战南征 提交于 2019-12-11 00:31:53

问题


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.


回答1:


Here follows the approach I got to reach this goal:
1) Create a Windows Forms Application called WindowsFormsApplication7, Use the latest Framework.
2) Make sure to include all references


3) Add a Class with the following code.


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


6) Add an Workflow Activity called Activity1.xaml


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


9) Add the following Arguments lbl (Label) and txt (string) to the Activity1


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


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

12) Congrats!!!



来源:https://stackoverflow.com/questions/8523694/simple-app-using-windows-workflow-and-winforms-not-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!