轉帖|其它|編輯:郝浩|2011-08-31 15:12:17.000|閱讀 810 次
概述:上一篇,我用實例講解了在.NET Framework 3.0(3.5)中如何動態加載xoml創建和運行流程的做法。這一篇談一下在WF 4.0中的情況,首先介紹一下WF 4的一些重要變化.
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
上一篇,我用實例講解了在.NET Framework 3.0(3.5)中如何動態加載xoml創建和運行流程的做法。這一篇談一下在WF 4.0中的情況,首先介紹一下WF 4的一些重要變化.
1. WF 4中,默認就是用xaml(注意,不是xoml),同時不允許包含c#代碼
2. WF 4中,不再區分順序工作流和狀態機工作流
3. WF 4中,不再能直接使用Code Activity,如果希望寫代碼,則需要編寫一個自定義的Activity,繼承Code Activity
我接下來還是用一個例子講解一下如何在WF 4中動態加載xaml工作流的做法吧
1. 創建自定義的Activity
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添加到流程中
設置它的Text屬性
3.將工作流的屬性進行一些修改
注意,將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="//schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:local="clr-namespace:WorkflowConsoleApplication1" xmlns:mc="//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="//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="//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"));
}
}
}
運行上述代碼,我們會遇到一個錯誤
這是為什么呢?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="//schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:local="clr-namespace:WorkflowConsoleApplication1;assembly=WorkflowConsoleApplication1" xmlns:mc="//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="//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="//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>
我個人認為這應該算是一個bug。但目前的情況就是這樣,如果你的自定義Activity是在當前應用程序里面,則也是需要設置Assembly的信息的。
當然,如果自定義Activity是單獨的Assembly,則應該默認就會寫上Assembly信息,那種情況反而是沒有問題的。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客園