Andrew
Andrew

Reputation: 423

Saving in the WF4 WorkflowDesigner

How do I save the XAML for a WF4 workflow in a rehosted designer, without writing it to a file? I want to store the serialised workflow in a database as an XML string.

Given a WorkflowDesigner instance called w, this saves to a file fine:

WorkflowDesigner.Flush();
w.Save("filename.xaml");

I was hoping this would serialise to a string - but it fails:

WorkflowDesigner.Flush();
var modelService = WorkflowDesigner.Context.Services.GetService<ModelService>();
var workflow = modelService.Root;
var xml = XamlServices.Save(workflow);

... while saving a single Sequence activity, it says "Type 'System.Activities.Presentation.Model.ModelItemImpl' not visible. If the type is local, please set the LocalAssembly field in XamlReaderSettings.

Upvotes: 2

Views: 2377

Answers (3)

Isaac Yuen
Isaac Yuen

Reputation: 71

Using ActivityXamlServices and a StringWriter to flush the content to a string.

Upvotes: 1

Maurice
Maurice

Reputation: 27632

Yes, use Flush() and Text to get the xaml as a string. Use Save() to save to a file or stream, no Flush() needed in that case.

Upvotes: 3

user1228
user1228

Reputation:

Hmmm, I haven't used this; the API seems a bit... odd. But from the docs, it appears that when you call Flush() on the instance of the WorkflowDesigner, it saves the current workflow to a public property called Text. You should be able to grab that text and stick it in your database just fine.

From the docs:

Before getting this value, call Flush() to convert the current workflow to text. After setting this value, call Load() to load the XAML into the workflow.

Upvotes: 2

Related Questions