1.Creating the first WF program:HelloWorkflow
HelloWorkflow is a Workflow Console Application. The workflow we created in WF Designer is actually an XML file. We can open Workflow1.xaml with an XML editor to check it.
In Visual Studio 2010 we can Right-click on Workflow1.xaml then click “檢視代碼” open Workflow1.xaml as an XML file.
So far, there are no officially published WF4 Designer add-ins for Visual Studio 2008. We need a copy of Visual Studio 2010 installed on our computer to use WF4 Designer, otherwise we can only create workflows by imperative code or by writing pure XAML files.
HelloWorkflow是一個“工作流控制台應用程式”,我們在工作流設計器中建立的工作流其實就是一個XML檔案,在VS2010中我們可以通過以下方法檢視工作流的XML檔案:右鍵Workflow1.xaml,選擇“檢視代碼”。到目前為止,隻有VS2010有WF4的設計器,VS2008中還沒有WF4編輯器的插件,是以如果要在VS2008中開發WF4,那麼就隻能通過編寫純XAML文檔的方式進行。
2.Creating a WF program using C# Code
HelloCodeWorkflow is a Console Application.這個項目是通過純代碼的方式來建立WF4項目。具體見代碼,也可以參考書本。
3.Initializing a WF program using InArguments
UseInArgument is a Workflow Console Application.在這個項目中主要是建立了InArgument類型的參數。可以為輸入參數傳遞變量。
4.Creating a WF program using OutArgument
UseOutArgument is a Workflow Console Application.OutArgument是輸出參數類型,在工作流中可以作為輸出參數用。在宿主應用程式中可以通過特定的方法輸出該參數中的内容。
The WorkflowInvoder.Invoke method will return a IDictionary type object.
WorkflowInvoder.Invoke方法會傳回IDictionary類型對象。
There is a third type of workflow argument: InOutArgument. It is a binding terminal that represents the flow of data into and out of an activity. In most cases, we can use InOutArgument instead of InArgument and OutArgument. But there are still some differences—for example, we cannot assign a string to InOutArgument, while it is allowed to
assign a string to InArgument directly in the host program.
在大多數情況下,可以用InOutArgument參數類型來代替InArgument和OutArgument。但是也有一些例外情況,比如我們可以直接将一個string類型的變量指派為InArgument類型的參數,但是卻不能在宿主程式中直接将string字元串指派給InOutArgument類型的參數。
5.Creating a WF program using InOutArgument
In this task, we will create a WF program using InOutArgument. This type of argument is used to receive values and is also used to pass values out to the caller (WF host).
InOutArgument變量既可以用來擷取值,也可以用來向WF宿主輸出值。
UseInOutArgument is a Workflow Console Application.
6.Using Variable in a WF program
UseVariable is Workflow Console Application.
We can use Variable temporarily to store a value when a WF program is running. In this task,we will create a WF program that prints five numbers to the console in a loop. We will use the NumberCounter variable as a number counter.
在WF程式運作期間,我們可以通過變量(Variable)來暫時儲存一個值。
While we can use arguments to flow data into and out of a workflow, we use Variable to store data in a workflow. Every variable has its scope, and can be accessed by activities within its scope. Variable in WF4 is pretty much like variables in imperative language such as C#.
7.Running a WF program asynchronously
UseWorkflowApplication a Workflow Console Application.
In the previous tasks, we used the WorkflowInvoker.Invoke method to start a workflow instance on the same thread as the main program. It is easy to use; however, in most real
applications, a workflow should run on an independent thread. In this task, we will use
WorkflowApplication to run a workflow instance.
通過使用WorkflowInvoker.Invoke方法來啟動一個工作流執行個體。工作流和宿主程式是在同一個線程上運作。這樣用起來雖然友善,當時在大多數實際的應用程式中,都是将宿主程式與工作流程式的線程分開。讓工作流在一個獨立的線程中運作。
As the workflow thread runs simultaneously with the caller thread, the caller thread may terminate before the workflow thread.To prevent this unexpected program quit,we need to use AutoResetEvent to synchronize caller and workflow thread.
如果工作流線程和調用者線程同時運作,調用者線程可能在工作流線程之前結束 為了防止這種意想不到的程式退出,我們需要通過使用AutoResetEvent來使得 調用者工作流線程異步
8.Customizing a MyReadLine activity with Bookmark
MyReadLineActivity a Workflow Console Application.
By using , we can flow data into the workflow when it starts and out of the workflow when it ends. But how can we pass data from the caller into the workflow when it is executing?—Bookmark will help us to do this. In this task, we will create a MyReadLine activity using a bookmark.
可以通過參數InArgument, OutArgument, and InOutArgument來使得資料在工作流中流動。但是我們如何在工作流執行期間從調用程式向工作流傳遞資料呢。Bookmark可以幫助我們完成這樣的任務。
In WF4, workflow is actually an Activity class. We could see "Workflow" as a conception from a macroeconomic viewpoint, while considering "Activity" as a development concept.
在WF4中,工作流實際上就是一個Activity類。我們可以将工作流了解為宏觀上的概念,而活動則是開發上的概念。
9.Converting a WF program instance to XAML
ConvertWFInstanceToXML a Workflow Console Application.
In real applications, we would like to write and test WF programs in imperative code, while storing, running, and transmitting workflow as an XAML string or file. In this task, we will convert a WF program instance to an XAML string.
将工作流程式執行個體轉換成XAML類型的字元串。
Consider the following code line:
XamlServices.Save(xw, ab);
XamlServices provides services for the common XAML tasks of reading XAML and writing an object graph, or reading an object and writing out an XAML file. This statement reads an ActivityBuilder object and writes XAML to an XamlWriter object.
We use ActivityBuilder as an activity wrapper so that the output XAML is a loadable workflow. In other words, if we save, say, a Sequence activity to an XamlWriter directly, then the output XML workflow will be unloadable for further use.
10.Loading up a WF program from an XAML file
LoadUpWorkflowFromXML a Workflow Console Application.
In this task, we will run a WF program by loading it from an XAML file.
在本執行個體中,通過加載XAML檔案來運作工作流應用程式。
11.Testing a WF program with a unit test framework
UnitTestForWFProgram is a Test Project.
[TestClass] indicates it is a unit test class, whereas [TestMethod] indicates a test method. When the Test Project runs, the test method will be executed automatically.
[TestClass]表示這個一個單元測試類。[TestMethod]表示這是一個測試類。當測試項目運作的時候,測試方法會自動執行。
12.Debugging a WF program
DebugWFProgram a Workflow Console Application.
作者:xwdreamer