- Microsoft Windows Workflow Foundation 4.0 Cookbook
- Andrew Zhu
- 466字
- 2021-04-13 17:00:36
By using InArgument
, OutArgument
, and InOutArgument
, 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.
- Create a workflow project:
Create a new Workflow Console Application under the
Chapter01
solution and name the project asUseBookmark
. Next, add a code file to this project and name the file asMyReadLineActivity
. We can see this in the following screenshot: - Customize the activity with Bookmark:
Fill the opening
MyReadLineActivity.cs
file with the following code:using System.Activities; namespace UseBookmark{ public class MyReadLine : NativeActivity<string>{ [RequiredArgument] public InArgument<string> BookmarkName { get; set; } protected override void Execute( NativeActivityContext context) { context.CreateBookmark(BookmarkName.Get(context), new BookmarkCallback(OnResumeBookmark)); } protected override bool CanInduceIdle { get { { return true;} } } public void OnResumeBookmark( NativeActivityContext context, Bookmark bookmark, object obj) { Result.Set(context, (string)obj); } } }
Save the file and press F6 to build the project so that the activity will appear in the WF designer activity toolbox.
- Author a workflow:
Open
Workflow1.xaml
and author the workflow as shown in the following screenshot: - Write code to host the workflow:
Open
Program.cs
file and change the code as follows:using System; using System.Linq; using System.Activities; using System.Activities.Statements; using System.Threading; namespace UseBookmark{ class Program{ static void Main(string[] args) { AutoResetEvent syncEvent = new AutoResetEvent(false); string bookmarkName="GreetingBookmark"; WorkflowApplication wfApp = new WorkflowApplication(new Workflow1() { BookmarkNameInArg=bookmarkName }); wfApp.Completed = delegate( WorkflowApplicationCompletedEventArgs e) { syncEvent.Set(); }; wfApp.Run(); wfApp.ResumeBookmark(bookmarkName, Console.ReadLine()); syncEvent.WaitOne(); } } }
- Run it:
Set
UseBookmark
asStartup
project. Press Ctrl+F5 to build and run the workflow without debugging. The application should run in a console window and print the message as shown in the following screenshot:
In the code shown in the second step, we create a class inherited from NativeActivity
. NativeActivity
is a special abstract activity that can be used to customize complex activities; we will talk about it more in Chapter 5, Custom Activities.
context.CreateBookmark(BookmarkName.Get(context), new BookmarkCallback(OnResumeBookmark));
By this statement, the WF context creates a Bookmark with arguments BookMarkName
and BookMarkCallback
. When the wfApp.ResumeBookmark
method is called, the OnResumeBookmark
that was defined in the Customized Activity body will be executed.
protected override bool CanInduceIdle{ get { { return true;} } }
This is a built-in property that indicates whether the customized activity can cause the workflow to become idle; the default value is false
.
Consider the following code snippet of step 3:
wfApp.ResumeBookmark(bookmarkName, Console.ReadLine());
When this statement is executed, the OnResumeBookmark
method defined in the MyReadLine
activity will be called and the method will accept the value passed via Console.ReadLine()
.
- UG NX 12.0中文版完全自學(xué)一本通
- Moodle 1.9 for Teaching 7/14 Year Olds: Beginner's Guide
- Photoshop CS6 商業(yè)應(yīng)用案例實戰(zhàn)
- Wordpress 3 Complete
- ASP.NET 3.5 Application Architecture and Design
- 印象筆記留給你的空間2.0:個人知識管理實踐指南
- Google App Engine Java and GWT Application Development
- AutoCAD 2017從新手到高手
- SolidWorks2016中文版從入門到精通/CAX工程應(yīng)用叢書
- 音樂日記:Studio One 6場景×風(fēng)格編曲實用教程
- PPT設(shè)計與制作實戰(zhàn)教程
- UG NX12中文版實用教程
- 金融精英PPT實操手冊:世界知名公司這樣展示研究報告
- Magento 1.3 Theme Design
- Creo 5.0從入門到精通(第2版)