The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

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!

www.TheProblemSolver.nl
Wiki.WindowsWorkflowFoundation.eu

Published Sat, Aug 28 2010 16:01 by Maurice
Filed under: , , ,

Comments

# re: Converting a C# workflow into XAML@ Tuesday, August 31, 2010 1:55 PM

Will this code convert the hosted service workflow?

by Gufran Sheikh

# re: Converting a C# workflow into XAML@ Wednesday, October 27, 2010 4:08 AM

Great :-)

by Felice