2011-05-20 67 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