开发者

Validate an Activity from within an ActivityDesigner?

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 c开发者_如何学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?


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.


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???

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜