แก้ไข

แชร์ผ่าน


WPF and Windows Workflow Foundation integration in XAML

The WPFWFIntegration sample demonstrates how to create an application that uses Windows Presentation Foundation (WPF) and Windows Workflow Foundation (WF) features in a single XAML document. To accomplish this, the sample uses Windows Workflow Foundation and XAML extensibility.

Sample details

The ShowWindow.xaml file deserializes into a Sequence activity with two string variables that are manipulated by the sequence's activities: ShowWindow and WriteLine. The WriteLine activity outputs to the console window the expression that it assigns to the Text property. The ShowWindow activity displays a WPF window as part of its execution logic. The DataContext of the window includes the variables declared in the sequence. The controls of the window declared in the ShowWindow activity use data binding to manipulate those variables. Finally, the window contains a button control. The Click event for the button is handled by a ActivityDelegate named MarkupExtension that contains a CloseWindow activity. MarkUpExtension invokes the contained activity that provides, as context, any objects identified by an x:Name, as well as the DataContext of the containing window. Thus, the CloseWindow.InArgument<Window> can be bound using an expression that references the window's name.

The ShowWindow activity derives from the AsyncCodeActivity<TResult> class to display a WPF window and completes when the window is closed. The Window property is of type Func<Window> that allows the window to be created on demand for each execution of the activity. The Window property uses a XamlDeferringLoader to enable this deferred evaluation model. The FuncFactoryDeferringLoader allows a XamlReader to be captured during serialization and then read during activity execution.

A well-written activity never blocks the scheduler thread. However, the ShowWindow activity cannot complete until the window it is displaying is closed. The ShowWindow activity achieves this behavior by deriving from AsyncCodeActivity, calling the BeginInvoke method in the BeginExecute method, and showing the window modally. The delegate is invoked through the WPF SynchronizationContext. The ShowWindow activity assigns the DataContext property to the Window.DataContext property to provide any data bound controls access to the in-scope variables.

The last point of interest in this sample is a MarkupExtension called DelegateActivityExtension. The ProvideValue method of this markup extension returns a delegate that invokes an embedded activity. This activity runs in an environment that includes the WPF data context and any x:Name values in scope. In the GenericInvoke method, this environment is provided to the activity through a SymbolResolver extension. This extension is added to a WorkflowInvoker that is then used to invoke the embedded activity whenever the markup extension's delegate is invoked.

Note

The default designer does not support the ShowWindow activity; as such, the ShowWindow.Xaml file does not display correctly in the designer.

Run the sample

  1. Using Visual Studio, open the WPFWFIntegration.sln solution file.

  2. To build the solution, press Ctrl+Shift+B.

  3. To run the solution, press F5.

  4. Type your first and last name into the dialog.

  5. Close the dialog and the console echoes your name.