2011-12-15 34 views
2

我正在尋找一個簡單的初學者應用程序,允許您鍵入一個值1 - 10此值傳遞給一個WF規則,如果它是更大,小於或等於5並將結果返回到窗體應用程序,該應用程序將結果顯示在標籤中。簡單的應用程序使用Windows工作流和winforms不是控制檯

我可以找到很多.net 3.5控制檯應用程序教程,但沒有顯示如何傳入並接收使用Windows窗體和.net 4的結果!

它不需要是上面的例子,但它需要告訴我如何將值傳遞給規則,編寫規則並從規則中讀取規則從.net中的Windows窗體應用程序4 c# 。

我迷路了!

我的基本代碼現在工作的情況下它可以幫助別人:

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(); 

名字是在傳給工作流動方向的爭論。 問候語是在工作流程中分配方向的參數。

+0

你到目前爲止編碼了什麼...? – MethodMan 2011-12-15 17:17:56

+0

+1好問題,wf4需要更多的愛。然而,sl msdn vidz很好。 – 2012-07-25 00:44:15

回答

0

這裏如下我實現這一目標的方法:
1)創建一個Windows窗體稱爲WindowsFormsApplication7應用程序,使用最新的框架。 Create a Windows Forms Application as usual
2)確保包括所有引用
enter image description here


3)添加班級用下面的代碼。在形式

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)雙擊和與這一個替換的代碼。不要介意錯誤。它們將消失。

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的
enter image description here


6)添加稱爲Activity1.xaml

enter image description here

的工作流活動


7)編譯解決方案(F6)。的UpdateLabel活動,如在Class1的(公共類UpdateLabel:CodeActivity)描述必須存在於工具箱

8)從工具箱,將UpdateLabel和的WriteLine活動納入活性1
enter image description here
enter image description here
9)以下參數LBL(標籤),TXT(串)添加到活動1


10)在UpdateLabel活動,按F4(屬性)點擊一次,更新活動的參數如圖所示
enter image description here


11)按F5編譯並運行應用程序。在文本框中插入一些文本,然後按按鈕。文本必須顯示在標籤中,由Activity1更新,並在輸出窗口中由WriteLine活動更新
enter image description here
12)恭喜!!!

相關問題