任务 2:创建“简单的订单窗体”Windows 窗体
在此任务中,将创建教程中所使用的宿主应用程序。 该应用程序是一个将 Windows 窗体用于其用户界面的基本订单窗体。 通过该应用程序,用户可以从列表中选择项目并添加数量值。
执行此任务之后,您便会拥有支持此功能的应用程序。 在本教程的所有剩余部分中,将添加其余所需的代码,用于调用状态机工作流并与其进行通信。
备注
虽然建议您按顺序进行下列练习,但并不要求您这么做。 您可以通过打开示例项目并执行下节中所示的步骤来开始此练习。
创建 Windows 窗体源代码文件
按照以下步骤创建订单窗体并将代码添加到宿主应用程序中。
如果使用的是 Visual Studio,则会在任务 1 中自动为您创建 Windows 窗体源代码文件(Program.cs 或 Program.vb)。
若要使用文本编辑器创建该文件,请执行以下步骤。
创建 Windows 窗体源代码文件
在项目目录中,创建一个名为 Program 的新文件。
如果要创建 C# 应用程序,请为该文件指定 .cs 扩展名;如果要创建 Visual Basic 应用程序,则为文件指定 .vb 扩展名。
在主项目文件 (StateMachineWorkflow) 中,将新的 ItemGroup 元素插入到文件末尾的 Import 元素的前面。
在 ItemGroup 元素中添加一个新的 Compile 元素。
向名为 Include 的 Compile 元素添加一个新属性,并使用在步骤 1 中创建的文件名作为该属性的值。
向 Compile 元素添加一个名为 SubType 的新的子元素。
为此元素指定 Form 值。 ItemGroup 节点将如下所示。
<ItemGroup> <Compile Include="Program.vb"> <SubType>Form</SubType> </Compile> </ItemGroup>
<ItemGroup> <Compile Include="Program.cs"> <SubType>Form</SubType> </Compile> </ItemGroup>
将 Windows 窗体代码添加到宿主应用程序中
在 Program 源代码文件中,为 Windows 窗体应用程序添加下面的代码。
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Windows.Forms; using System.Workflow.Runtime; using System.Workflow.Runtime.Hosting; using System.Workflow.Activities; namespace Microsoft.Samples.Workflow.Tutorials.StateMachineWorkflow { public class MainForm : Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox itemsList; private System.Windows.Forms.Label label2; private System.Windows.Forms.NumericUpDown itemQuantity; private System.Windows.Forms.Button submitButton; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.ComboBox ordersIdList; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox orderStatus; private System.Windows.Forms.Label label4; private System.ComponentModel.IContainer components = null; private Dictionary<string, List<string>> orderHistory; private string[] inventoryItems = { "Apple", "Orange", "Banana", "Pear", "Watermelon", "Grapes" }; private delegate void ItemStatusUpdateDelegate(Guid orderId, string newStatus); public MainForm() { InitializeComponent(); // Initialize the inventory items list foreach (string item in this.inventoryItems) { this.itemsList.Items.Add(item); } this.itemsList.SelectedIndex = 0; // Initialize the order history collection this.orderHistory = new Dictionary<string, List<string>>(); } private string GetOrderHistory(string orderId) { // Retrieve the order status StringBuilder itemHistory = new StringBuilder(); foreach (string status in this.orderHistory[orderId]) { itemHistory.Append(status); itemHistory.Append(Environment.NewLine); } return itemHistory.ToString(); } private void submitButton_Click(object sender, EventArgs e) { } private void ordersIdList_SelectedIndexChanged(object sender, EventArgs e) { this.orderStatus.Text = GetOrderHistory(this.ordersIdList.SelectedItem.ToString()); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.itemsList = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.itemQuantity = new System.Windows.Forms.NumericUpDown(); this.submitButton = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.orderStatus = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.ordersIdList = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.itemQuantity)).BeginInit(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(23, 13); this.label1.TabIndex = 0; this.label1.Text = "Item"; // // this.itemsList // this.itemsList.DropDownStyle = ComboBoxStyle.DropDownList; this.itemsList.FormattingEnabled = true; this.itemsList.Location = new System.Drawing.Point(13, 26); this.itemsList.Name = "itemsList"; this.itemsList.Size = new System.Drawing.Size(161, 21); this.itemsList.TabIndex = 0; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 50); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(42, 13); this.label2.TabIndex = 2; this.label2.Text = "Quantity"; // // this.itemQuantity // this.itemQuantity.Location = new System.Drawing.Point(13, 67); this.itemQuantity.Minimum = new decimal(new int[] { 1, 0, 0, 0}); this.itemQuantity.Name = "itemQuantity"; this.itemQuantity.Size = new System.Drawing.Size(80, 20); this.itemQuantity.TabIndex = 1; this.itemQuantity.Value = new decimal(new int[] { 1, 0, 0, 0}); // // this.submitButton // this.submitButton.Location = new System.Drawing.Point(99, 64); this.submitButton.Name = "submitButton"; this.submitButton.Size = new System.Drawing.Size(100, 23); this.submitButton.TabIndex = 2; this.submitButton.Text = "Submit Order"; this.submitButton.Click += new System.EventHandler(this.submitButton_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.orderStatus); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.ordersIdList); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Location = new System.Drawing.Point(13, 93); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(247, 193); this.groupBox1.TabIndex = 3; this.groupBox1.TabStop = false; this.groupBox1.Text = "Order Status"; // // orderStatus // this.orderStatus.Location = new System.Drawing.Point(7, 82); this.orderStatus.Multiline = true; this.orderStatus.Name = "orderStatus"; this.orderStatus.ReadOnly = true; this.orderStatus.Size = new System.Drawing.Size(230, 105); this.orderStatus.TabIndex = 3; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(8, 65); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(35, 13); this.label4.TabIndex = 2; this.label4.Text = "History"; // // ordersIdList // this.ordersIdList.DropDownStyle = ComboBoxStyle.DropDownList; this.ordersIdList.FormattingEnabled = true; this.ordersIdList.Location = new System.Drawing.Point(7, 37); this.ordersIdList.Name = "ordersIdList"; this.ordersIdList.Size = new System.Drawing.Size(230, 21); this.ordersIdList.TabIndex = 0; this.ordersIdList.SelectedIndexChanged += new System.EventHandler(this.ordersIdList_SelectedIndexChanged); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(7, 20); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 13); this.label3.TabIndex = 0; this.label3.Text = "Order Id"; // // MainForm // this.AcceptButton = this.submitButton; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(272, 298); this.Controls.Add(this.groupBox1); this.Controls.Add(this.submitButton); this.Controls.Add(this.itemQuantity); this.Controls.Add(this.label2); this.Controls.Add(this.itemsList); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "MainForm"; this.Text = "Simple Order Form"; ((System.ComponentModel.ISupportInitialize)(this.itemQuantity)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } } static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainForm()); } } }
编译代码
有关编译代码的信息,请参见编译代码。
在下一练习练习 2:创建“简单的订单窗体”应用程序中,将定义一个用于启用状态机工作流与简单的订单窗体应用程序之间的通信的接口。
请参见
概念
其他资源
练习 2:创建“简单的订单窗体”应用程序
教程:创建状态机工作流
Ordering State Machine
Simple State Machine
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。