WF4B1 的有返回值的Activity,Bookmark,有返回值Bookmark

WF4B1 的有返回值的Activity,Bookmark,有返回值Bookmark

 

目录

1    NativeActivity<T>    1

1.1    定义有返回值的Activity    1

1.2    使用    1

2    Bookmark说明    2

3    Bookmark    2

3.1    定义Activity    2

3.2    定义工作流    2

3.3    宿主    3

3.4    结果    3

4    Bookmark(有返回值)    3

4.1    定义Activity    3

4.2    定义工作流    4

4.3    宿主    4

4.4    结果    5

 

 

NativeActivity<T>

 

类名:System.Activities.NativeActivity<TResult>

基类:System.Activities.WorkflowElement<TResult>

文件: System.Activities.dll

类型:abstract (void Execute(System.Activities.ActivityExecutionContext context))

 

定义有返回值的Activity

场景说明:有两个[Int]参数v1,v2,返回值为v1+v2

 

public class myActivity<T> : NativeActivity<int>

{

public System.Activities.InArgument<int> v1

{ set; get; }

 

public System.Activities.InArgument<int> v2

{ set; get; }

 

protected override void Execute(ActivityExecutionContext context)

{

int r =(int)v1.Get(context) + (int)v2.Get(context);

this.Result.Set(context, r);

}

}

 

说明:

当在设计器中添加有返回值的Activity时,会出现一个对话框,要求指定返回值的类型

 

使用

流程

宿主

WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());

 

myInstance.Run();

 

System.Console.Read();

结果

 

Bookmark说明

Bookmark可使用流程Idle并触发流程的OnIdle

Bookmark

 

 

要定义具备Bookmark的Activit,可从 NativeActivity继承

 

类名:System.Activities.NativeActivity

基类:System.Activities.WorkflowElement

文件: System.Activities.dll

类型:abstract (void Execute(System.Activities.ActivityExecutionContext context))

 

定义Activity

 

public class myBookmark : NativeActivity

{

 

public string bookmarkName { get; set; }

protected override void Execute(ActivityExecutionContext context)

{

context.CreateNamedBookmark(bookmarkName);

}

}

 

 

定义工作流

<p:Activity mc:Ignorable=""

x:Class="WorkflowConsoleApplication1.Sequence2"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"

xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:w="clr-namespace:WorkflowConsoleApplication1;assembly=WorkflowConsoleApplication1"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

>

<p:Sequence >

<p:WriteLine DisplayName="WriteLine:begin">["begin"]</p:WriteLine>

<w:myBookmark bookmarkName="mywait" />

<p:WriteLine DisplayName="WriteLine:end">["end"]</p:WriteLine>

</p:Sequence>

</p:Activity>

 

宿主

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<StackPanel>

<Button Content="启动" Height="23" Name="button1" Width="75" Click="button1_Click" />

<Button Content="继续" Height="23" Name="button2" Width="75" Click="button2_Click" />

 

</StackPanel>

 

</Window>

WorkflowInstance myInstance;

private void button1_Click(object sender, RoutedEventArgs e)

{

myInstance = new WorkflowInstance(new Sequence2());

myInstance.OnIdle += idel;

myInstance.Run();

 

}

IdleAction idel()

{

System.Console.WriteLine("等待提交.....");

return IdleAction.Nothing;

}

 

private void button2_Click(object sender, RoutedEventArgs e)

{

myInstance.ResumeBookmark("mywait",null);

}

结果

 

Bookmark(有返回值)

要定义具备有返回值的Bookmark Activit,可从 NativeActivity<T>继承

 

类名:System.Activities.NativeActivity<TResult>

基类:System.Activities.WorkflowElement<TResult>

文件: System.Activities.dll

类型:abstract (void Execute(System.Activities.ActivityExecutionContext context))

 

定义Activity

public sealed class bookmark<T> : NativeActivity<T>

{

public bookmark()

: base()

{

}

 

public string bookmarkName { get; set; }

 

protected override void Execute(ActivityExecutionContext context)

{

context.CreateNamedBookmark(bookmarkName, new BookmarkCallback(bookmarkCallback));

 

}

 

void bookmarkCallback(ActivityExecutionContext context, Bookmark bookmark, object obj)

{

this.Result.Set(context, (T)obj);

 

}

}

 

定义工作流

<p:Activity mc:Ignorable=""

x:Class="myBookmarks.Sequence1"

xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities/design"

xmlns:m="clr-namespace:myBookmarks;assembly=myBookmarks"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:p="http://schemas.microsoft.com/netfx/2009/xaml/activities"

xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<p:Sequence>

<p:Sequence.Variables>

<p:Variable x:TypeArguments="x:String" Name="rs" />

</p:Sequence.Variables>

<p:WriteLine DisplayName="开始">["请提交意见"]</p:WriteLine>

<m:bookmark x:TypeArguments="x:String" DisplayName="等待意见" Result="[rs]" bookmarkName="mywait" />

<p:WriteLine DisplayName="显示结果">[rs]</p:WriteLine>

</p:Sequence>

</p:Activity>

 

宿主

<Window x:Class="WpfApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Canvas>

<Button Canvas.Left="27" Canvas.Top="31" Content="启动流程" Height="23" Name="button1" Width="75" Click="button1_Click" />

<Button Canvas.Left="27" Canvas.Top="111" Content="继续" Height="23" Name="button2" Width="75" Click="button2_Click" />

<TextBox Canvas.Left="123" Canvas.Top="112.157" Height="23" Name="textBox1" Width="120" />

</Canvas>

</Window>

WorkflowInstance myInstance;

private void button1_Click(object sender, RoutedEventArgs e)

{

myInstance = new WorkflowInstance(new Sequence1());

myInstance.OnIdle += idel;

myInstance.Run();

}

 

IdleAction idel()

{

System.Console.WriteLine("等待提交.....");

return IdleAction.Nothing;

}

 

private void button2_Click(object sender, RoutedEventArgs e)

{

myInstance.ResumeBookmark("mywait", this.textBox1.Text);

}

 

结果

 

 

 

 

 

posted @ 2009-06-08 12:58  WXWinter(冬)  阅读(1331)  评论(2编辑  收藏  举报