ActivityValidator.Validate(ValidationManager, Object) 方法

定義

驗證指定的活動是否有效。

public override System.Workflow.ComponentModel.Compiler.ValidationErrorCollection Validate (System.Workflow.ComponentModel.Compiler.ValidationManager manager, object obj);

參數

manager
ValidationManager

與驗證相關聯的 ValidationManager

obj
Object

要驗證的 Activity

傳回

ValidationErrorCollection

ValidationErrorCollection 物件,包含驗證期間發生的任何錯誤或警告。

範例

下列範例示範如何針對自訂活動 (此活動具備名為 Validate 且型別為 ActivityValidator 的單一相依屬性) 所用的 Msg,覆寫其所使用的 String 方法。 自訂驗證程式可確保有設定 Msg 屬性。 若未設定,編譯器會在 Validate 上呼叫 ActivityValidator 方法時顯示錯誤,編譯也將失敗。

public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
{
    // Invoke the base class method implementation to
    // perform default validation.
    ValidationErrorCollection errors = base.Validate(manager, obj);

    // Make sure there is an activity instance.
    ConsoleWriteLineActivity crw = obj as ConsoleWriteLineActivity;
    if (crw == null)
    {
        throw new InvalidOperationException();
    }

    // If the activity has no parent then this validation
    // is occurring during the compilation of the activity
    // and not during the hosting or creation of an
    // activity instance.
    if (crw.Parent == null)
    {
        // Can skip the rest of the validation because
        // it deals with the hosting and the creation
        // of the activity.
        return errors;
    }

    // Msg is required. Add a validation error if there is no
    // Msg specified or Msg is not bound to another property.
    if (string.IsNullOrEmpty(crw.Msg) &&
        crw.GetBinding(ConsoleWriteLineActivity.MsgProperty) == null)
    {
        errors.Add(new ValidationError("Msg is required", 100, false, "Msg"));
    }

    return errors;
}

備註

這個方法呼叫 ValidateProperties 以驗證 Activity 的屬性。

如果 Name 不是根 Activity,則這個方法只會驗證 ActivityActivity 的唯一性。

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8