一起谈.NET技术,WF4.0中如何实现XAML工作流的动态加载

  我接下来还是用一个例子讲解一下如何在WF 4中动态加载xaml工作流的做法吧。

  1. 创建自定义的Activity

image

 
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

namespace WorkflowConsoleApplication1
{

public sealed class MyActivity : CodeActivity
{
// Define an activity input argument of type string
public InArgument < string > Text { get ; set ; }

// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context)
{
// Obtain the runtime value of the Text input argument
string text = context.GetValue( this .Text);

Console.WriteLine(text);
}
}
}

  2.将这个自定义的Activity添加到流程中

image  设置它的Text属性:

image  3.将工作流的属性进行一些修改

image  注意,将BuildAction设置为Content,同时Copy to Output Directory 设置为Copy always,并且将Custom Tool设置为空白,完成操作之后,得到的xaml文件如下:

 
  
< Activity mc:Ignorable ="sap" x:Class ="WorkflowConsoleApplication1.Workflow1" sap:VirtualizedContainerService.HintSize ="240,240" mva:VisualBasic.Settings ="Assembly references and imported namespaces for internal implementation" xmlns ="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:local ="clr-namespace:WorkflowConsoleApplication1" xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv ="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva ="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s ="clr-namespace:System;assembly=mscorlib" xmlns:s1 ="clr-namespace:System;assembly=System" xmlns:s2 ="clr-namespace:System;assembly=System.Xml" xmlns:s3 ="clr-namespace:System;assembly=System.Core" xmlns:sad ="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap ="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg ="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1 ="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2 ="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3 ="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd ="clr-namespace:System.Data;assembly=System.Data" xmlns:sl ="clr-namespace:System.Linq;assembly=System.Core" xmlns:st ="clr-namespace:System.Text;assembly=mscorlib" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" >
< local:MyActivity sad:XamlDebuggerXmlReader.FileName ="D:\temp\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml" sap:VirtualizedContainerService.HintSize ="200,200" Text ="Hello,World" />
</ Activity >

  4. 通过下面的代码创建并且运行流程

 
  
using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Activities.XamlIntegration;

namespace WorkflowConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
WorkflowInvoker.Invoke(ActivityXamlServices.Load(
" workflow1.xaml " ));
}
}
}

  运行上述代码,我们会遇到一个错误。

image  这是为什么呢?MyActivity找不到?我们应该手工将xaml文件成下面这样。请注意粗体的部分,我添加了assembly的设置:

 
  
< Activity mc:Ignorable = " sap " x:Class = " WorkflowConsoleApplication1.Workflow1 " sap:VirtualizedContainerService.HintSize = " 240,240 " mva:VisualBasic.Settings = " Assembly references and imported namespaces for internal implementation " xmlns = " http://schemas.microsoft.com/netfx/2009/xaml/activities " xmlns:local = " clr-namespace:WorkflowConsoleApplication1;assembly=WorkflowConsoleApplication1 " xmlns:mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 " xmlns:mv = " clr-namespace:Microsoft.VisualBasic;assembly=System " xmlns:mva = " clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities " xmlns:s = " clr-namespace:System;assembly=mscorlib " xmlns:s1 = " clr-namespace:System;assembly=System " xmlns:s2 = " clr-namespace:System;assembly=System.Xml " xmlns:s3 = " clr-namespace:System;assembly=System.Core " xmlns:sad = " clr-namespace:System.Activities.Debugger;assembly=System.Activities " xmlns:sap = " http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation " xmlns:scg = " clr-namespace:System.Collections.Generic;assembly=System " xmlns:scg1 = " clr-namespace:System.Collections.Generic;assembly=System.ServiceModel " xmlns:scg2 = " clr-namespace:System.Collections.Generic;assembly=System.Core " xmlns:scg3 = " clr-namespace:System.Collections.Generic;assembly=mscorlib " xmlns:sd = " clr-namespace:System.Data;assembly=System.Data " xmlns:sl = " clr-namespace:System.Linq;assembly=System.Core " xmlns:st = " clr-namespace:System.Text;assembly=mscorlib " xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml " >
< local:MyActivity sad:XamlDebuggerXmlReader.FileName = " D:\temp\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml " sap:VirtualizedContainerService.HintSize = " 200,200 " Text = " Hello,World " />
</ Activity >

image  我个人认为这应该算是一个bug。但目前的情况就是这样,如果你的自定义Activity是在当前应用程序里面,则也是需要设置Assembly的信息的。当然,如果自定义Activity是单独的Assembly,则应该默认就会写上Assembly信息,那种情况反而是没有问题的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值