|
WPF based applications work like WinForm ones with respect to OpenSpan’s integration technologies. There are a couple of changes to make for those who have already worked with WinForm’s.
The following assumes you already know about OpenSpan adapters and about WPF, this is by no means an introduction to coding applications with WPF.
Windows Presentation Foundation (WPF) is a Microsoft technology that uses Extensible Markup Language (XAML - rhymes with Camel) files to produce modern user interfaces for applications.
The example given here is the same one as done in the article “HTML Tables with C#” at http://www.openspan.com/Community/index.php/code-gallery/80-html-tables-with-c.html except this time it’s done using WPF and XAML rather than with WinForms. Please read that article if you haven’t already since this one focuses only on the WPF aspects of working with OpenSpan.
Changes to make when going from a WinForm to a WPF application
There are three main areas where working with OpenSpan differs when using WPF instead of WinForms.
- UI Properties
- Event Arguments
- Threading
UI Property changes are the easiest. WPF has many similar graphical concepts as WinForms but WPF is both richer and more flexible. You often use labels in a WinForm application. In WPF, a label is similar expect that instead of just showing text, a WPF label can have images, video etc embedded. Instead of having a .Text property, the same is accomplished in WPF using the label.Content property such as:
this.label1.Content = HtmlTable.matchCell_Product.Text;
which sets the text of label1 to whatever the text of the match cell is.
Event Arguments
Another change between WinForm and WPF is that the arguments to events has changed. Usually the simplest thing to do is use Visual Studio’s autocomplete feature to fill it in for you.
What was in WinForm:
private void Form1_FormClosed( object sender, FormClosedEventArgs e )
Is now in WPF, though largely this is just different names since the main “form” is now a window and the event pattern is a bit different.
private void Window_Closed( object sender, EventArgs e )
Threading
The biggest difference has been left for last. The WPF model for threading is designed to make writing responsive applications easier than with WinForm. MSDN has an article describing this called Build More Responsive Apps With The Dispatcher. You can also read the article, Starting Adapters and Multi-Threading which discusses threading when working with OpenSpan adapters.
Regardless of if you are using WPF or WinForms, you need to ensure any events are handled on the UI thread if they are using the UI controls. With a WinForm application, you can either start the adapter with a synchronization object, usually a form or you handle it in code using with Invoke() method on a control such as:
if ( this.InvokeRequired ) { this.Invoke( new updateUI( handleToolbarUpdate ), keyText ); return; }
However you can’t use the adapter.Start( synchronizationObject ) method in WPF since the WPF windows and controls use a different class hierarchy. This may be changed in future versions of the adapter but for now, you need to handle marshaling threads in code. The pattern is almost the same as in WinForm. While you can consider it just a different set of names, you should read the above mentioned MSDN article to get the finer details on how threading is improved in WPF.
Instead of using the control’s InvokeRequired property and Invoke method, you now use the CheckAccess() method and Dispatcher.Invoke() method as below. The CheckAccess() method will not show up in Intellisense but it will compile.
public delegate void Delegate();
private void GetDataFromTable() { // The CheckAccess() method is part of most UI objects however it is // hidden from Intellisense as of 2010. // See http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/845005894931 if ( !CheckAccess() ) { Dispatcher.Invoke( new Delegate( GetDataFromTable ), null ); return; }
The attached solution shows the full code for this. To write it, we took the WinForm solution and replaced the UI and simply made a few changes for event names and threading
|