Converting a C# workflow into XAML

A interesting question that came up last week was how to convert workflows defined in C# to XAML.

A co worker of one of the attendees of the Essential Windows Workflow Foundation 4 course had been experiencing a lot of problems with the workflow designer and decided to create their workflows in C# instead of using the designer to generate XAML. While these workflows run just fine you do lose the visual aspect of the designer, one of the benefits of workflow in the first place.

Fortunately it isn’t hard to save workflow objects, however they have been authored, into their XAML representation using the XamlServices.

Take the following workflow defined in C#.

var workflow = new Sequence();


workflow.Activities.Add(new WriteLine() { Text = "Hello workflow." });


workflow.Activities.Add(new Persist());


workflow.Activities.Add(new If()


{


    Condition = new VisualBasicValue<bool>("System.DateTime.Now.Hour < 12"),


    Then = new WriteLine() { Text = "Good morning" },


    Else = new WriteLine() { Text = "Good afternoon" }


});


workflow.Activities.Add(new WriteLine()


{


    Text = new VisualBasicValue<string>("\"The current time is: \" & System.DateTime.Now.ToLongTimeString()")


});

This will run just fine as is but if I want to convert this into XAML all I need is a single line of code:

XamlServices.Save(@"..\..\demo.xaml", workflow);



And when it runs I get a nice XAML file with the graphical representation of the original C# workflow.



image



And now I can continue enhancing the XAML version and run that instead.



 



Enjoy!



[f1]
[f2]

2 thoughts on “Converting a C# workflow into XAML

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>