views:

319

answers:

2

Hello all,

I have Googled a bit, and cannot seem to find any examples of Xaml-fying Activities - good, bad, or otherwise!

public static string ToXaml (this Activity activity)
{
    // i would use ActivityXamlServices to go from Xaml
    // to activity, but how to go other way? documentation
    // is slim, and cannot infer proper usage of 
    // ActivityXamlServices from Xml remarks :S
    string xaml = string.Empty;
    return xaml;
}

Hints, tips, pointers would be welcome :)


NOTE: so found this. Will work through and update once working. Anyone wanna beat me to the punch, by all means. Better yet, if you can find a way to be rid of WorkflowDesigner, seems odd it is required.

+3  A: 

Alright, so worked through this forum posting.

You may Xaml-fy [ie transform an instance to declarative Xaml] a well-known Activity via

public static string ToXaml (this Activity activity)
{
    StringBuilder xaml = new StringBuilder ();

    using (XmlWriter xmlWriter = XmlWriter.Create (
        xaml, 
        new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true, }))

    using (XamlWriter xamlWriter = new XamlXmlWriter (
        xmlWriter, 
        new XamlSchemaContext ()))

    using (XamlWriter xamlServicesWriter = 
        ActivityXamlServices.CreateBuilderWriter (xamlWriter))
    {
        ActivityBuilder activityBuilder = new ActivityBuilder 
        {
            Implementation = activity
        };
        XamlServices.Save (xamlServicesWriter, activityBuilder);
    }

    return xaml.ToString ();
}

Your Xaml may contain certain artifacts, such as references to System.Activities.Presentation namespace appearing as xmlns:sap="...". If this presents an issue in your solution, read the source link above - there is a means to inject directives to ignore unrecognized namespaces.

Will leave this open for a while. If anyone can find a better solution, or improve upon this, please by all means :)

johnny g
This doesn't seem to work anymore with VS2010 RC. The constructor for XamlWriter is now protected. If I found another solution, I'll post it here.
Ronald Wildenberg
hm, i will double check what i have posted here, but i am pretty sure i had this exact snippet running in VS2010RC. `XamlWriter` is abstract, however in the above snippet, i instantiate 1) a `XamlXmlWriter` which is public concrete, and 2) some descendant of `XamlWriter` via public factory method `ActivityXamlServices.CreateBuilderWriter`
johnny g
yep, this is VS2010RC compliant. did you perhaps misread `XamlXmlWriter` for abstract `XamlWriter`? i know i'm guilty of that one - misread some sample source and hit my head for a week!
johnny g
A: 

Based on the other solution (for VS2010B2) and some Reflectoring, I found a solution for VS2010RC. Since XamlWriter is abstract in the RC, the new way to serialize an activity tree is this:

public static string ToXaml (this Activity activity)
{
    var xamlBuilder = new StringBuilder();
    var xmlWriter = XmlWriter.Create(xamlBuilder,
        new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
    using (xmlWriter)
    {
        var xamlXmlWriter =
            new XamlXmlWriter(xmlWriter, new XamlSchemaContext());
        using (xamlXmlWriter)
        {
            XamlWriter xamlWriter =
                ActivityXamlServices.CreateBuilderWriter(xamlXmlWriter);
            using (xamlWriter)
            {
                var activityBuilder =
                    new ActivityBuilder { Implementation = sequence };
                XamlServices.Save(xamlWriter, activityBuilder);
            }
        }
    }
    return xamlBuilder.ToString();
}
Ronald Wildenberg