5

我正在学习 Windows Workflow Foundation 4,并尝试创建以下程序:

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 包含一个调用 Activity1 的操作。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>
4

3 回答 3

4

事实证明,我需要手动引用生成的 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 本身中编译工作流。一旦它们在图书馆中,我就如上所述引用它们。

于 2012-06-29T21:54:46.977 回答
0

由于您的 xaml 不是松散的 xaml,而是带有代码(类)的 xaml,因此您不能自行加载 xaml。

x:Class="Activity1"尝试从Activity元素中删除属性。
- 如果您的活动在后面的代码中没有成员(方法、字段、属性),那么您实际上并不需要后面的代码。

(属性相同x:Class="Workflow1"。)

编辑:

xmlns:local=或任何其他名称空间,例如, xmlns:src=)属性仅用于引用自定义或系统名称空间中的类型,而不是默认 ( )。xmlns:x=xmlns:srd=xmlns=

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

您在一个地方使用本地命名空间:<local:Activity1 sap:VirtualizedContainerService.HintSize="200,22" />,因此您确实需要映射,但是,您将不得不更正它。(例如xmlns:local=MyCompany.MyProject.SubNameSpace,假设您引用的 DLL 与 xaml 所在的 DLL 相同。)

如果您希望 xaml 加载为松散 xaml,您还需要将程序集名称添加到定义中。例如xmlns:mms=MyCompany.MyProject.SubNameSpace;AssemblyName- 不是local,因为松散的 xamls 没有本地 ns。)

于 2012-06-16T00:24:14.433 回答
0

在您使用主要功能的项目中引用所有依赖的dll(用于开发活动和工作流)。

于 2012-06-21T12:02:15.010 回答