技术开发 频道

详解Visual Studio 2010中的活动设计器

  【IT168 技术文档】正如你已经看到的, Windows工作流允许你使用代码创建自定义活动。根据不同的需求,你可以使用几种不同的类。

  1.打开解决方案

  你需要根据以下步骤:

  (1)开启Microsoft Visual Studio 2010。

  (2)在%TrainingKitInstallFolder%\Labs\ IntroToWF\Ex9-ActivityDesigner\Begin目录下打开解决方案。

  (3)按CTRL+SHIFT+B来编译解决方案。

  2.创建自定义的NativeActivity

  在这个练习中,我们会创建一个简单的自定义活动,它具有Pre/Post processing的能力。

  (1)按CTRL+SHIFT+B来编译解决方案。

  (2)右击HelloWorkflow解决方案文件并选择Add / New Project…

  (3)选择Workflow模板并点击Activity Library. 输入项目名HelloWorkflow.Activities


加一个叫做HelloWorkflow.Activities的新Activity Library项目

  (4)删除 Activity1.xaml,因为这次练习中将不需要它。

  (5)右击HelloWorkflow.Activities 并选择Add / NewItem (Ctrl+Shift+A)

  (6)从Workflow模板中选择Code Activity并将它取名为PrePostSequence


添加一个名为PrePostSequence的新Code Activity

  (7)PrePostSequence类将作为其它活动的容器。我们需要对模板提供的代码做一些修改。删除这个类的内容,用以下代码替代。

  (代码段- Introduction to WF4 Lab – PrePostSequence Class CSharp)

  public sealed class PrePostSequence : NativeActivity

  {

  
public Activity Pre { get; set; }

  
public Activity Post { get; set; }

  
public List Activities { get; set; }

  
public PrePostSequence()

  {

  Activities
= new List();

  }

  
protected override void Execute(NativeActivityContext context)

  {

  
// Schedule the activities in order

  context.ScheduleActivity(Pre);

  Activities.ForEach((a)
=> { context.ScheduleActivity(a); });

  context.ScheduleActivity(Post);

  }

  }

  (代码段
- Introduction to WF4 Lab – PrePostSequence Class VB)

  Visual Basic

  Public NotInheritable Class PrePostSequence

  Inherits NativeActivity

  Public Property Pre As Activity

  Public Property Post As Activity

  Public Property Activities As List(Of Activity)
= New List(Of Activity)

  Protected Overrides Sub Execute(ByVal context

  As System.Activities.NativeActivityContext)

  context.ScheduleActivity(Pre)

  For Each Activity In Activities

  context.ScheduleActivity(Activity)

  Next

  context.ScheduleActivity(Post)

  End Sub

  End Class

   (8)按CTRL+SHIFT+B编译解决方案

  (9)HelloWorkflow 项目会需要引用HelloWorkflow.Activities项目。将HelloWorkflow.Activities加入HelloWorkflow的引用中。

  (10)在设计器中打开SayHello.xaml,并注意工具栏现在包含我们的PrePostSequence活动了。

  注意

  在Visual Studio 2010 Beta 2中有一个已知的问题,会使自定义活动无法出现在工具栏中。如果这样的状况发生了,尝试在Solution Explorer中删除Solution Items文件夹,然后重新编译。如果这还不行,手工把那个自定义活动加入工具栏中。

  3.将自定义活动拖至设计器表面

  (1)将PrePostSequence活动放置在Finally之后。现在我们需要创建自定义活动设计器。


没有自定义设计器的PrePostSequence活动

  (2)将PrePostSequence活动从设计器表面删除

  (3)保存并关闭SayHello.xaml

  4.创建自定义活动设计器

  在这个练习中,我们将为PrePostSequence 活动创建一个自定义活动设计器

  (1)右击HelloWorkflow 解决方案,并选择Add / New Project

  (2)选择Workflow 模板并点击Activity Designer Library。将项目取名为HelloWorkflow.Activities.Designers


添加一个新的Activity Designer Library

  (3)删除ActivityDesigner1.xaml,因为我们不会需要它。

  (4)右击HelloWorkflow.Activities.Designers 项目并选择Add / New Item

  (5)从Workflow 模板选择Activity Designer 并将之取名文为PrePostSequenceDesigner


添加PrePostSequenceDesigner activity designer

  (6)将模板中的XAML文件替换成以下内容

  (代码段- Introduction to WF4 Lab – PrePostSequenceDesigner XAML CSharp)

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

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

  xmlns:sap
="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"

  xmlns:sapv
="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">

  Activities

  (代码段
- Introduction to WF4 Lab – PrePostSequenceDesigner XAML VB)

  XAML (VB)

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

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

  xmlns:sap
="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"

  xmlns:sapv
="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">

  Item
="{Binding Path=ModelItem.Pre, Mode=TwoWay}"

  HintText
="Insert Pre Activities Here"/>

  BorderThickness
="2" CornerRadius="5">

  HorizontalAlignment
="Center">Activities

  Items
="{Binding Path=ModelItem.Activities}"

  HintText
="Insert Activities Here">

  WorkflowItemPresenter
/ WorkflowItemsPresenter

 

  我们的自定义设计器使用databinding来绑定到PrePostSequence类的属性。 Pre和Post属性是单一的活动,所以自定义设计器使用WorkflowItemPresenter将它们呈现在设计器表面。

  活动集合使用WorkflowItemsPresenter来创建设计器表面,它可以包含一个活动的集合。

  (7)按CTRL+SHIFT+B来编译解决方案

  5.将活动设计器与活动练习起来

  现在我们有了一个活动设计器,但除非将它与自定义活动练习起来,它才能发挥作用。

  (1)右击HelloWorkflow.Activities项目并选择Add Reference…

  (2)从projects tab加入对HelloWorkflow.Activities.Designers的引用

  (3)再一次选择Add Reference,并从.NET tab里加入对以下程序集的引用

  ·System.Activities.Presentation

  ·PresentationFramework

  ·PresentationCore

  ·WindowsBase

  (4)打开PrePostSequence.cs (C#)或PrePostSequence.vb (VB),并加入以下命名空间

  using System.ComponentModel;

  
using HelloWorkflow.Activities.Designers;
  Imports System.ComponentModel

  
Imports HelloWorkflow.Activities.Designers

 

  (5)在PrePostSequence类上添加以下属性.

  [Designer(typeof(PrePostSequenceDesigner))]

  
public sealed class PrePostSequence : NativeActivity

 

  Visual Basic

  
Public NotInheritable Class PrePostSequence

 

0
相关文章