Tag WPF

WPF Reading List

I’m currently putting together a session on how to get started in WPF (Windows desktop client application) development for my team and I’ve assembled a list of resources for experienced developers who are new to C#, the .NET Framework, and WPF. Just wanted to share for all those who are interested in ramping up or taking it to the next level.

Books on C# and .NET

  • Joseph and Ben Albahari, C# 4.0 in a Nutshell. For those who are new to C#, but are not new to programming. Covers the language only, concise.
  • Andrew Troelsen, Pro C# 2010 and the .NET 4 Platform. For programmers who are new to the language, surveys the base libraries in .NET (file access, database access, and network access).
  • Jeffrey Richter, CLR via C#, Third Edition. A systems-level view of the language and the .NET platform. Assumes basic knowledge of C# and digs deep to explain what’s going on under the hood.
  • Jon Skeet, C# in Depth, Second Edition. Dives into the design and evolution of the language.

Books on WPF

  • Adam Nathan, WPF 4 Unleashed. In full color, clear and concise, the best book on WPF (and I’ve read them all).
  • Matthew MacDonald, Pro WPF in C# 2010. A close second: more examples, but a little bit on the bigger side.

Articles on Essential Concepts in WPF (read in order)

  1. WPF Architecture
  2. Routed Events Overview
  3. Data Binding Overview

Books on Software Design

  • Krzysztof Cwalina & Brad Abrams, Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, Second Edition. The design guidelines used to create the .NET Framework, useful guidance for creating your own libraries.
  • Elisabeth and Eric Freeman, Head First Design Patterns. A very accessible read to start grokking the basics of design patterns, which are a staple in WPF/MVVM application development.

Books on Interaction Design

  • Jenifer Tidwell, Designing Interfaces, Second Edition. In full color, a catalog of modern user interface patterns, with narrative that explains how and when to use the pattern, and why it works. A great resource for getting practical ideas to solve GUI design problems.
  • Donald Norman, The Design of Everyday Things. An accessible introduction to usability and human factors concepts. A quick read to put oneself into a user-oriented mindset.
  • Alan Cooper, Robert Reimann, & David Cronin, About Face 3: The Essentials of Interaction Design. Varsity level, but deep insight into designing effective user interfaces.
  • Ben Shneiderman, Catherine Plaisant, Maxine Cohen, & Steven Jacobs, Designing the User Interface: Strategies for Effective Human-Computer Interaction, Fifth Edition. A wide, but in depth survey of modern user interfaces, including direct manipulation (NUI), virtual environments, command languages, distributed interfaces,
  • Christopher Wickens & Justin Hollands, Engineering Psychology and Human Performance, Third Edition. Graduate-level text on human factors for engineering systems. Challenging, but no other book like it. Covers perception, spatial displays, real and virtual environments, language, memory, decision making, attention, workload, multitasking, stress, and error in the context of complex, real-world systems.
  • Edward Tufte, Visual Display of Quantitative Information, Envisioning Information, Visual Explanations, Beautiful Evidence. Well-crafted books that illustrate analytical principles of design and how to represent multivariate ideas and information in two-dimensional space, whether it be on paper or on a screen.

Hosting WPF Content in an MFC App

I recently had to host a WPF control in an MFC application. These are my notes on how to do it, along with some extras near the end.

MSDN has an article, WPF and Win32 Interoperation Overview, that describes how to host a WPF control in a Win32 application. The instructions on Win32 hosting WPF are quite good, and there’s a code example in Walkthrough: Hosting WPF Content in a Win32 Application.

MFC is essentially a wrapper for part of the Win32 API. As a result, the procedure for hosting a WPF control in an MFC app is very similar to the procedure for hosting a WPF control in a pure Win32 app. The following is a mapping of the steps listed in the WPF-Win32 interop article to the steps I took in MFC.

1. Create WPF content.

2. Implement a Win32 application with C++/CLI.
Implement an MFC app. (I created a new one as a test app, dialog-based for simplicity.) Then, set the MFC app to compile with managed code (with /clr).

  • Right click on the MFC project in the Solution Explorer.
  • Configuration Properties → General
  • Set Common Language Runtime support option to Common Language Runtime Support /clr.

With this option set, the MFC app now has access to the .NET Framework Library via C++/CLI syntax. In addition, add references in the MFC app (right click on the MFC app in the Solution Explorer, References) to the following four .NET assemblies for WPF support:

  • WindowsBase
  • PresentationCore
  • PresentationFramework
  • System

3. Set threading model to STA (single threaded apartment).
In project properties, go to Configuration Properties → Linker → Advanced → CLR Thread Attribute Property to STA threading attribute (/CLRTHREADATTRIBUTE:STA).

4. Handle the WM_CREATE notification in your window.
Using the IDE, add a handler for the WM_CREATE message. The IDE creates function OnCreate().

5. Within the handler

  1. Create a new HwndSource object with the parent window HWND as its parent parameter.
  2. Create an instance of your WPF content.
  3. Assign a reference to the WPF content object to the HwndSource object RootVisual property.
  4. The HwndSource object’s Handle property contains the window handle (HWND). To get an HWND that you can use in the unmanaged part of your application, cast Handle.ToPointer() to an HWND.

6. Implement a managed class that contains a static field that holds a reference to your WPF content object.

  • Allows you to get a reference to the WPF content object from your Win32 code.
  • Prevents your HwndSource from being inadvertently garbage collected.

7. Receive notifications from the WPF content object by attaching a handler to one or more of the WPF content object events.
There is a good example, with code, from Microsoft Support where they attach an MFC-style handler to the WPF control’s cut and paste events: Hosting a WPF Control in MFC and Enable Cut-and-Paste. I used this sample code as a model to implement steps 5, 6, and 7.

8. Communicate with the WPF content object by using the reference you stored in the static field to set properties, call methods, etc.

For the steps I took above, I used Visual Studio 2008 SP1, .NET Framework 3.5, and MFC 9.0. I also used a slide deck on WPF Interop from Jaime Rodriguez, who was one of the presenter/instructors in the WPF LOB Training Tour I attended a few months back. The following are some quick bullet notes I took in his “Tips and Tricks” session on WPF Interop. (These are my notes I took while Jaime was presenting, so it is a mix of what he presented and what I was thinking. They are not a direct transcription of what Jaime presented.)

  • A Win32 app hosting a WPF control needs to be compiled with /clr.
  • No nested hooks: can’t do something like host WPF in WinForms in Win32 or host WPF in Win32 in WPF. No guarantees.
  • Issue: WinForms control dialogs always come up on top. Workaround: WPF popup (window) that shows over the WinForms control.
  • Example of Win32 hosted in WPF: Microsoft Expression Design. The canvas is Win32, everything else is WPF.
  • Obviously, hosting Win32 or WinForms in WPF allows for reuse of complex code that does not need to be rewritten.
  • Another example of Win32 hosted in WPF: Blockbuster Video app. WPF app contains Windows Media Player, which is Win32. Rationale: Windows Media Player performance is currently better than WPF video player because Windows Media Player is more advanced—team has been developing it longer, it uses the hardware better.
  • Although WinForms and WPF interoperate (via a mechanism like WinFormsHost in WPF), cannot directly convert WinForms designs to WPF designs because WinForms designs serialize to code, while WPF designs serialize to XAML.

Most of the examples and discussion out there addresses hosting Win32 components in WPF app shells because new apps from scratch are generally written in WPF, with reuse of Win32 components (likely drawing components written using GDI that does not need to be rewritten immediately to support DirectX). Hosting WPF in Win32/MFC is less common, but does provide a migration path toward WPF when has an existing application but wants to make use of some of the new GUI technology. One thing to take into consideration in both cases is planning the layout (and considering the “airspace”). Designate regions of the app (like a document/drawing canvas or a major control panel) to be Win32 while the remainder of the app is in WPF, rather than mixing and matching Win32 and WPF ad hoc, a Win32 button here, a WPF button there.

References
Microsoft. (n.d.). WPF and Win32 Interoperation Overview. Retrieved on July 7, 2009, from http://msdn.microsoft.com/en-us/library/ms742522.aspx#.

Microsoft. (n.d.). Walkthrough: Hosting Windows Presentation Foundation Content in a Win32 Application. Retrieved on July 7, 2009, from http://msdn.microsoft.com/en-us/library/ms744829.aspx.

Microsoft. (2008). Hosting a WPF Control in MFC and Enable Cut-and-Paste. Retrieved on July 7, 2009, from http://support.microsoft.com/kb/959082.

Miranda, T. (2009). WindowsClient.NET: Video Training – Host a WPF Control in a Win32 Application. Retrieved on July 7, 2009, from http://windowsclient.net/learn/video.aspx?v=13252.

Rodriguez, J. (2009). Jaime Rodriguez on .NET Client: Thanks Chicago. Here is the Content. Retrieved on July 9, 2009, from http://blogs.msdn.com/jaimer/archive/2009/06/16/thanks-chicago-here-is-the-content.aspx.

Sells, C., & Griffiths, I. (2007). Programming WPF: Building Windows UI with Windows Presentation Foundation (2nd ed.). Sebastopol, CA: O’Reilly Media.

Follow

Get every new post delivered to your Inbox.