如何在WF 4.5中为声明性工作流添加约束?

时间:2014-08-15 16:48:16

标签: constraints workflow-foundation

是否有任何关于如何在Windows Workflow Foundation 4.5工作流中创建声明性约束的示例?我查看了samples on MSDN,并且样本中没有任何声明性约束的例子。

1 个答案:

答案 0 :(得分:-1)

Creating a Constraint Library is not that hard: 请查看以下复制代码:

public static class CustomRulesLibrary
{


    public static ValidationSettings Settings = new ValidationSettings()
                                 {
                                     AdditionalConstraints =
                                             {
                                                 {typeof (Activity), new List<Constraint> {MustBeSomething()}},
                                             }
                                 }; 


    public static Constraint MustBeSomething()
    {
        var element = new DelegateInArgument<Activity>();

        return new Constraint<Activity>
        {
            Body = new ActivityAction<Activity, ValidationContext>
            {
                Argument1 = element,
                Handler = new AssertValidation
                {
                    IsWarning = true,
                    Assertion = new InArgument<bool>(env => (element.Get(env).DisplayName.Length > 2)),
                    Message = new InArgument<string>("Some custom message"),
                }
            }
        };            
    }
}

希望以上有所帮助。