0

我对 Windows 工作流程真的很陌生,我需要创建一个活动。我这样做了:

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; }
}

现在我想检查用户在使用设计器时在此属性中设置的内容。例如,如果他编译工作流,编译工作流时是否有任何回调,所以我可能会产生编译错误?或任何完整性检查回调?

谢谢你的帮助。

4

1 回答 1

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
    ...
}
于 2012-11-29T17:27:44.307 回答