NativeActivity Base Class
NativeActivity is an abstract class with a protected constructor. Like CodeActivity, NativeActivity is used for writing imperative behavior by implementing an Execute method. Unlike CodeActivity, NativeActivity has access to all of the exposed features of the workflow runtime through the NativeActivityContext object passed to the Execute method.
Using NativeActivityContext
Features of the workflow runtime can be accessed from within the Execute method by using members of the context
parameter, of type NativeActivityContext. The features available through NativeActivityContext include the following:
Getting and setting of arguments and variables.
Scheduling child activities with ScheduleActivity
Aborting activity execution using Abort.
Canceling child execution using CancelChild and CancelChildren.
Access to activity bookmarks using such methods as CreateBookmark, RemoveBookmark, and ResumeBookmark.
Custom tracking features using Track.
Access to the activity’s execution properties and value properties using GetProperty and GetValue.
Scheduling activity actions and functions using ScheduleAction and ScheduleFunc.
To create a custom activity that inherits from NativeActivity
OpenVisual Studio 2010.
Select File, New, and then Project. Select Workflow 4.0 under Visual C# in the Project Types window, and select the v2010 node. Select Activity Library in the Templates window. Name the new project HelloActivity.
Right-click Activity1.xaml in the HelloActivity project and select Delete.
Right-click the HelloActivity project and select Add, and then Class. Name the new class HelloActivity.cs.
In the HelloActivity.cs file, add the following
using
directives.using System.Activities; using System.Activities.Statements;
Make the new class inherit from NativeActivity by adding a base class to the class declaration.
class HelloActivity : NativeActivity
Add functionality to the class by adding an Execute method.
protected override void Execute(NativeActivityContext context) { Console.WriteLine("Hello World!"); }
Override the CacheMetadata method and call the appropriate Add method to let the workflow runtime know about the custom activity’s variables, arguments, children, and delegates. For more information see the NativeActivityMetadata class.
Use the NativeActivityContext object to schedule a bookmark. See Bookmarks for details on how to create, schedule, and resume a bookmark.
protected override void Execute(NativeActivityContext context) { // Create a Bookmark and wait for it to be resumed. context.CreateBookmark(BookmarkName.Get(context), new BookmarkCallback(OnResumeBookmark)); }