Validate an Activity from within an ActivityDesigner?

久未见 提交于 2019-12-07 20:40:24

问题


I'd like to validate the workflow within the design surface (in this case, Visual Studio) from within one of the child Activities' designer. I'd like to prevent users from moving forward until other errors are corrected in order to simplify the design experience later down the road.

The naiive implementation doesn't work:

var activity = (this.ModelItem.Root.GetCurrentValue() as ActivityBuilder)
                    .Implementation as Activity;
var validationResult = ActivityValidationServices.Validate(activity);
if (validationResult.Errors.Count > 0))
{
    MessageBox.Show("The Workflow is invalid.  Fix it.", "Derp");
    return;
}

The problem is that the ActivityBuilder (which is not an Activity and cannot be passed to Validate) contains any and all Variables and Arguments defined on the root of the workflow. So, when you attempt to validate the first child of the root (Implementation), you get invalid Errors as any bindings to these fail.

I've seen suggested hacks where you add the Implementation to a carrier Activity (say, Sequence) and then add all Variables and Arguments found in the ActivityBuilder to the carrier.

This stinks.

Is there a better way?


回答1:


I don't think there is. I am guessing your Google searched turned up this question on the forums where Tim came up with pretty much the same result.

Given that the ModelItem is your activity adding a custom function to call and going your own checks in there might be a solutions. That way you can call the function from both the CacheMetadata and your activity builder.




回答2:


Hacks. Nothing but hacks.

var sb = new StringBuilder();
using (var tw = new StringWriter(sb))
using (var xw = ActivityXamlServices.CreateBuilderWriter(
                    new XamlXmlWriter(tw, new XamlSchemaContext())))
{
    XamlServices.Save(xw, 
                      this.ModelItem.Root.GetCurrentValue() as ActivityBuilder);
    tw.Flush();
}
using(var tr = new StringReader(sb.ToString()))
using (var xr = ActivityXamlServices.CreateReader(
                    new XamlXmlReader(tr, new XamlSchemaContext())))
{
    var activity = ActivityXamlServices.Load(xr);
    var validationResult = ActivityValidationServices.Validate(activity);
    if (!validationResult.IsValid())
    {
        MessageBox.Show("OMG what an awful hack.", "Validation Sucks");
        return;
    }
}

Can there be a better way to convert the ActivityBuilder to an Activity without serializing it???



来源:https://stackoverflow.com/questions/6050636/validate-an-activity-from-within-an-activitydesigner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!