15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1334 ❘ ChaPTer 44 windOws wOrkflOw fOundAtiOn 4<br />

}<br />

public UIElement ProperttInspectorView<br />

{<br />

get { return _designer.PropertyInspectorView; }<br />

}<br />

private WorkflowDesigner _designer;<br />

To begin, the ViewModel class derives from BaseViewModel — this base class is one that we use every time<br />

we construct a view model, as it provides an implementation of INotifyPropertyChanged. It comes from<br />

a set of snippets written by Josh Twist <strong>and</strong> available on www.thejoyofcode.com.<br />

The constructor ensures that the metadata for all of the built-in activities is registered — without this call,<br />

none of the type specific designers will show up on the user interface. Within the InitializeViewModel<br />

method, we then construct an instance of the Workflow Designer <strong>and</strong> load an activity into it. The<br />

WorkflowDesigner class is curious in that once you’ve loaded one workflow into it, you cannot load<br />

another, so here we re-create this class whenever a new workflow is created.<br />

The last thing that the InitializeViewModel method does is to call the property change notification<br />

function to indicate to the user interface that both the DesignerView <strong>and</strong> PropertyInspectorView are<br />

updated. As the UI is bound to these properties, they will be requeried <strong>and</strong> will load the new values from<br />

the new Workflow Designer instance.<br />

The next part of the user interface that needs to be created is the toolbox. In Workflow 3.x you had to construct<br />

this control yourself; however, in Workflow 4 there is a ToolboxControl, which is trivially easy to use.<br />

public UIElement Toolbox<br />

{<br />

get<br />

{<br />

if (null == _toolbox)<br />

{<br />

_toolbox = new ToolboxControl();<br />

ToolboxCategory cat = new ToolboxCategory<br />

("St<strong>and</strong>ard Activities");<br />

cat.Add(new ToolboxItemWrapper(typeof(Sequence),<br />

"Sequence"));<br />

cat.Add(new ToolboxItemWrapper(typeof(Assign), "Assign"));<br />

_toolbox.Categories.Add(cat);<br />

}<br />

ToolboxCategory custom = new ToolboxCategory<br />

("Custom Activities");<br />

custom.Add(new ToolboxItemWrapper(typeof(Message),<br />

"MessageBox"));<br />

_toolbox.Categories.Add(custom);<br />

}<br />

}<br />

return _toolbox;<br />

Here, we construct the toolbox control, then add two toolbox items to the first category <strong>and</strong> one toolbox<br />

item to a second category. The ToolboxItemWrapper class is used to simplify the code needed to add a<br />

given activity to the toolbox.<br />

With that code in place, we have a functioning application — well almost. All we need to do now is wire up<br />

the ViewModel with the XAML. This is done in the constructor for the main window.<br />

public MainWindow()<br />

{<br />

InitializeComponent();<br />

ViewModel vm = new ViewModel();<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!