2012-06-15 38 views
3

我正在學習有關Windows Workflow Foundation的4,並試圖創建下列程序:引用時XAML工作流

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

namespace BranchedActivities 
{ 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Activity wf = ActivityXamlServices.Load(@"C:\...\Workflow1.xaml"); 
      WorkflowInvoker.Invoke(wf); 

      Console.ReadKey(); 
     } 
    } 
} 

Workflow1包括一個單一的行動,呼籲活動1。 Activity1.xaml由單個Writeline組成。

當我將工作流加載爲編譯的Activity(通過使用「Activity wf = new Workflow1()」),程序加載完美。 當我嘗試加載活動的XAML(如上面的代碼)時,獲取異常:無法創建未知類型'{clr-namespace:} Activity1'。

我想我不得不以某種方式加載另一個xaml文件,雖然我很困難如何。

供您參考.... Workflow1.xaml:

<Activity mc:Ignorable="sap" x:Class="Workflow1" sap:VirtualizedContainerService.HintSize="262,240" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" 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" 
      xmlns:local="clr-namespace:"> 
    <Sequence sad:XamlDebuggerXmlReader.FileName="C:\...\Workflow1.xaml" sap:VirtualizedContainerService.HintSize="222,200"> 
    <sap:WorkflowViewStateService.ViewState> 
     <scg3:Dictionary x:TypeArguments="x:String, x:Object"> 
     <x:Boolean x:Key="IsExpanded">True</x:Boolean> 
     </scg3:Dictionary> 
    </sap:WorkflowViewStateService.ViewState> 
    <local:Activity1 sap:VirtualizedContainerService.HintSize="200,22" /> 
    </Sequence> 
</Activity> 

Activity1.xaml

<Activity mc:Ignorable="sap" x:Class="Activity1" sap:VirtualizedContainerService.HintSize="273,240" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" 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"> 
    <Sequence sad:XamlDebuggerXmlReader.FileName="C:\...\Activity1.xaml" sap:VirtualizedContainerService.HintSize="233,200"> 
    <sap:WorkflowViewStateService.ViewState> 
     <scg3:Dictionary x:TypeArguments="x:String, x:Object"> 
     <x:Boolean x:Key="IsExpanded">True</x:Boolean> 
     </scg3:Dictionary> 
    </sap:WorkflowViewStateService.ViewState> 
    <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="Hello World" /> 
    </Sequence> 
</Activity> 

回答

3

原來我需要手動引用生成的dll。我使用的工作代碼是:

XamlReader xamlReader; 
//Assembly wfAssembly = Assembly.GetExecutingAssembly(); 
Assembly wfAssembly = Assembly.LoadFile(@"Workflows.dll"); 
XamlXmlReaderSettings settings = new XamlXmlReaderSettings(); 
settings.LocalAssembly = wfAssembly; 
xamlReader = new XamlXmlReader(@"Workflow.xaml", settings); 
Activity wf = ActivityXamlServices.Load(xamlReader); 

我必須在visual studio自己編譯工作流程。一旦他們進入圖書館,我會像上面一樣引用他們。

+0

這對我構建GettingStarted示例WF應用 –

+0

[另一種方法是從dll加載所有已編譯的活動](https://gist.github.com/nickdevereaux/592ddffb38c5697b0c21)。在我的例子中,我有一個只編譯了工作流活動的目錄,但是你可以對任何目錄執行此操作,當然需要更長的時間 –

0

因爲你的XAML是不是一個鬆散XAML,而是背後的代碼XAML(一類),你不能自己加載xaml。

嘗試從Activity元素中刪除屬性x:Class="Activity1"
- 如果您的活動在後面的代碼中沒有成員(方法,字段,屬性),那麼您並不需要背後的代碼。

(同關於屬性x:Class="Workflow1"

編輯:

xmlns:local=(或任何其它的命名空間例如xmlns:src=xmlns:x=xmlns:srd=)屬性僅是使用到自定義或系統內的引用類型namesspace其他表示默認(xmlns=)。

xmlns:local="clr-namespace:"不是有效的命名空間映射,因爲它沒有映射到任何地方!

您在一個地方使用本地命名空間:<local:Activity1 sap:VirtualizedContainerService.HintSize="200,22" />,所以您確實需要映射,但是,您將太正確。 (例如,假設您引用的是與xaml相同的DLL)。

如果您希望將xaml加載爲鬆散的xaml,則還需要將程序集名稱添加到定義中。例如xmlns:mms=MyCompany.MyProject.SubNameSpace;AssemblyName - 不是local,因爲寬鬆xamls沒有本地NS)

+0

我從兩個xaml文檔中刪除了x:Class =屬性。但我仍然得到異常:「無法創建未知類型」{clr-namespace:} Activity1'。「 – Gaurav

+0

請參閱編輯我的答案。 –

0

把自己所有的相關的DLL(那些被用來開發項目的活動和工作流),您使用的主要功能。

+0

我試過了。但它似乎沒有考慮到這些引用 – Gaurav