2011-05-20 143 views
2

工作流程似乎是直接从Xaml创建的。那么如何在我的工作流类中包含System.Attribute?工作流程4.0和System.Attribute

我能想到的唯一途径是有点扯淡:

有用于相应的代码文件中的每个 Activity.xaml:

[MyCustomAttribute("hello")] 
public abstract class MyPointlessWorkflowBase : System.Activity 
{ 

} 

,然后有我的.xaml从基本继承(我甚至不知道这是否可能)?但这很糟糕,因为我需要为每个需要该属性的工作流添加一个额外的类。

是否有无论如何在您将.xaml放在它上面之前编写像他们是普通类一样的活动?

回答

3

XAML文件在编译之前会生成一个带有部分关键字的类,以便您可以创建具有相同名称的部分类并在其中添加该属性。

[MyCustomAttribute("hello")] 
public partial class MyWorkflow : Activity 
{ 
} 

或者,您可以使用x:ClassAttributes元素在XAML中添加一个属性,并以这种方式添加它们。

<p:Activity x:Class="WorkflowConsoleApplication1.MyWorkflow" 
      xmlns:s="clr-namespace:System;assembly=mscorlib" 
      xmlns:my="......" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <x:ClassAttributes> 
     <my:MyCustomAttribute> 
     <x:Arguments> 
      <s:String>hello</s:String> 
     </x:Arguments> 
     </my:MyCustomAttribute> 
    </x:ClassAttributes> 
</p:Activity> 
+0

yay for maurice! – peteisace 2011-05-20 09:03:13

+0

你真的做到了吗?编译器是否创建了'.g.'隐藏的部分类定义?并且它会嵌入'System.Windows.Markup.IComponentConnector'吗?你可以通过他们的'x:Name'来引用组件吗? – Will 2011-05-20 15:06:33

+0

我经常做第一种风格。其次,在XAML中,不是。有一个MyWorkflow.g.cs文件,但它不实现IComponentConnector。这也是生成属性的地方。 – Maurice 2011-05-20 16:21:55