选择 Workflow 模板并选择 活动库。将项目命名为 HelloWorkflow.Activities。

2、先删除 Activity1.xaml,然后右键单击 HelloWorkflow.Activities 项目并选择 添加 / 新建项 (Ctrl+Shift+A)
从 Workflow 模板中选择 代码活动 并将其命名为 PrePostSequence

3、 PrePostSequence 类原先内容:

把它删除,然后替换如下:

4、先建一个项目,选择 Workflow 模板并选择 活动设计器库。将项目命名为 HelloWorkflow.Activities.Designers

5、删除 ActivityDesigner1.xaml,右键单击 HelloWorkflow.Activities.Designers 项目并选择 添加 / 新建项
从 Workflow 模板中选择 活动设计器 并将其命名为 PrePostSequenceDesigner


6、生成的模板如下:

使用以下内容替换模板 XAML
<sap:ActivityDesigner x:Class="HelloWorkflow.Activities.Designers.PrePostSequenceDesigner"
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">
<StackPanel>
<Border BorderBrush="Green" BorderThickness="4" CornerRadius="5">
<sap:WorkflowItemPresenter Item="{Binding Path=ModelItem.Pre, Mode=TwoWay}" HintText="Insert Pre Activities Here"/>
</Border>
<Border BorderBrush="Red" BorderThickness="4" CornerRadius="5">
<StackPanel>
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="5">
<TextBlock HorizontalAlignment="Center">Activities</TextBlock>
</Border>
<sap:WorkflowItemsPresenter Items="{Binding Path=ModelItem.Activities}" HintText="Insert Activities Here">
<sap:WorkflowItemsPresenter.SpacerTemplate>
<DataTemplate>
<Ellipse Fill="Red" Width="30" Height="30" />
</DataTemplate>
</sap:WorkflowItemsPresenter.SpacerTemplate>
<sap:WorkflowItemsPresenter.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</sap:WorkflowItemsPresenter.ItemsPanel>
</sap:WorkflowItemsPresenter>
</StackPanel>
</Border>
<Border BorderBrush="Black" BorderThickness="4" CornerRadius="5">
<sap:WorkflowItemPresenter Item="{Binding Path=ModelItem.Post, Mode=TwoWay}" HintText="Insert Post Activities Here"/>
</Border>
</StackPanel>
</sap:ActivityDesigner>
其中红色字体是垂直方式,原文选择的是水平,下面左边可以改变方式,右边是展示的界面:

下图是替换完成后,按 CTRL+SHIFT+B 生成解决方案 后的展示:

7、从项目选项卡中,添加对 HelloWorkflow.Activities.Designers 的引用和添加以下命名空间指令及向 PrePostSequence 类添加以下属性

8、右键单击 HelloWorkflow 项目,然后选择 添加引用。从项目选项卡中选择 HelloWorkflow.Activities。按 CTRL+SHIFT+B 生成解决方案。
9、对HelloWorkflow.Tests 项目添加引用 HelloWorkflow.Activities

添加新测试:

10、添加 2 个新的输出参数来保存 PrePostSequence 的 Pre 和 Post 区域的消息

11、将 PrePostSequence 拖到 Finally 活动下方:

12、从工具箱中的基元组中将 Assign 活动拖到 Pre Activities 区域上并设置属性 To:PreMessage Value:"This is Pre-Sequence"
将另一个 Assign 活动拖到 Post Activities 区域上并设置属性 To:PostMessage Value:"This is Post-Sequence"

13、将两个 Assign 活动拖到 PrePostSequence 下方的红点活动区域中

14、按
CTRL+SHIFT+B 生成解决方案、按 Ctrl+R, A 运行所有测试,可以看到所有测试都能够通过
本文详细介绍如何在Visual Studio中创建一个自定义的工作流活动,包括建立项目、修改活动类和设计器类,以及如何在工作流中使用这些自定义活动。


被折叠的 条评论
为什么被折叠?



