15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

}<br />

{<br />

}<br />

Branches =<br />

{<br />

new PickBranch<br />

{<br />

Variables = { roomName, width, depth },<br />

Trigger = receiveRoomInfo,<br />

Action = new WriteLine { Text = "Room Info Received"},<br />

},<br />

new PickBranch<br />

{<br />

Trigger = receiveDetailsComplete,<br />

Action = new Sequence<br />

{<br />

Activities =<br />

{<br />

new Assign<br />

{<br />

To = new OutArgument(finished),<br />

Value = new InArgument(true)<br />

},<br />

new WriteLine { Text = "Finished" }<br />

}<br />

}<br />

}<br />

}<br />

Here, the While defines a Condition that evaluates the value of the finished variable. The Condition<br />

property is defined as an Activity, so the ExpressionServices helper class is used to convert the<br />

variable expression into an activity.<br />

The Pick contains two branches, <strong>and</strong> the Trigger activity of each branch is what we are waiting for in<br />

order to execute the Action activity. Building workflows in code is somewhat long-winded; however, we<br />

believe it’s easier to see what is going on when you are learning Workflow 4 than using XAML-based<br />

workflows. You might disagree!<br />

hosting the designer<br />

Often one wishes to save the best till last. We’d rather not break with tradition, so that’s what we’ve done<br />

with this chapter. The Workflow Designer that is used within Visual Studio can also be hosted within<br />

your own application, allowing your end users to create their own workflows without a copy of Visual<br />

Studio in sight. This is, we believe, the best feature of Workflow 4 by far. Traditional application extension<br />

mechanisms always require some form of developer — either to write an extension DLL <strong>and</strong> plug it into the<br />

system somewhere, or by writing macros or scripts. Windows Workflow allows end users to customize an<br />

application simply by dragging <strong>and</strong> dropping activities onto a design surface.<br />

Rehosting the designer in Workflow 3.x was not for the faint-hearted; however, in Workflow 4 it becomes<br />

almost trivial. The Designer itself is a WPF control, so we’ll use a WPF project as the main application.<br />

The code for this example is available in the 08_DesignerRehosting project.<br />

The first thing we need to do is to include the workflow assemblies, <strong>and</strong> then we need to define the main<br />

window XAML. We always use the Model-View-ViewModel (MVVM) pattern when constructing WPF user<br />

interfaces, as it simplifies the coding <strong>and</strong> also ensures that we can drape different XAML over the same view<br />

model if necessary. The XAML for the main window is as follows:<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!