2012-11-28 32 views
0

我對Windows工作流程非常陌生,我需要創建一個活動。我這樣做:如何檢查WCF活動中的屬性值?

class CustomActivity : Activity { } 

該活動定製屬性而我做了:

class CustomActivity : Activity 
{ 
    /// <summary> 
    /// Creation of the Value Property. 
    /// </summary> 
    [Description("The value of the property to set")] 
    [Category("Configuration")] 
    [Browsable(true)] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
    public string Value { get; set; } 
} 

現在我想檢查哪些用戶在這個屬性設置時,他使用了設計師。例如,如果他編譯工作流程,編譯工作流程時是否存在回調,那麼我可能會生成編譯錯誤?或者任何完整性檢查回調?

感謝您的幫助。

回答

1

我通過閱讀WF文檔找到了解決方案。這是很容易:

創建一個驗證器對象:

class CustomActivityValidator : ActivityValidator 
{ 
    public override ValidationErrorCollection ValidateProperties(ValidationManager manager, object obj) 
    { 
     if (null == manager) 
     { 
      throw new ArgumentNullException("manager"); 
     } 

     if (null == obj) 
     { 
      throw new ArgumentNullException("obj"); 
     } 

     CustomActivity activity = obj as CustomActivity; 
     if (null == activity) 
     { 
      throw new ArgumentException("This validator can only be used by the CustomActivity", "obj"); 
     } 

     ValidationErrorCollection errors = base.ValidateProperties(manager, obj); 
     if (null != activity.Parent) 
     { 
      // Now actually validate the activity... 
      if (activity.Value != "foobar") 
      { 
       ValidationError err = new ValidationError("This must be only foobar", 100, false, "Value"); 
       errors.Add(err); 
      } 
     } 

     return errors; 
    } 
} 

然後你這個驗證綁定到你的活動類

[ActivityValidator(typeof(CustomActivityValidator))] 
class CustomActivity : Activity 
{ 
    // Your activity code here 
    ... 
}