WF4.0中如何實現XAML工作流的動態加載

作者: 陳希章  來源: 博客園  發布時間: 2010-10-09 17:30  閱讀: 2732 次  推薦: 0   原文鏈接   [收藏]  

  我接下來還是用一個例子講解一下如何在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信息,那種情況反而是沒有問題的。

0
0
 
標簽:WF
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()