Introduction to WPF

This post is first among a series of tutorial posts that will teach WPF to beginners. The entire series will be divided into around 20-25 posts and each one should take you around 1/2 to 1 hour maximum to read and understand. If you follow them in the order that they have been written (from bottom to top), the series will not take more than a few days to complete. However, it is expected that you will have some basic background of .Net development and understanding of .Net framework before you begin these. If not, look forward to our next tutorial series on .Net development. All the code in these tutorials are written using Visual Studio 2012, and for database, we are going to use MS SQL 2008 R2. The code will work even on older versions of Visual Studio (those that support WPF) but you may have to change some things manually, and since it is not possible to list all the changes, we recommend using 2012 for a seamless learning experience. The express edition is free for trial.

This post will concentrate on the basic background information of WPF. This will be purely theoretical and if you already know the background, you can skip it. However, it is recommended that you at least glance through it once.

What is WPF?
WPF or Windows Presentation Foundation is a unified API (set of assemblies and tools) for building rich, sophisticated GUIs (Graphical User Interfaces) for desktop applications on .Net framework.

It is somewhat similar to windows forms but has much more advanced capabilities and makes creating graphical user interfaces much more simpler than forms. Also, it provides unified features for different UI tasks.

WPF is built on top of Directx so you can do lot of audio,video and multimedia tricks using WPF. Due to this, WPF takes advantage of hardware acceleration (removing the burden of graphical intensive processing from CPU on to some specialist hardware) where possible; as a result, the performance of WPF applications is often better when it comes to graphics.

WPF is available as part of .Net framework 3.0 onwards and works on Windows xp, vista, 7, 8 (and presumably future versions of windows too)

Main Features of WPF:
The main Features of WPF include:

  • Declarative UI – Use of XAML (Extensible Application Markup Language) helps in separating code from UI elements.
  • Intelligent layout – WPF provides intelligent layout capabilities to arrange the visual elements that can resize, adjust etc dynamically
  • Scalable graphics – WPF applications use vector graphics for many tasks, hence the graphics are scalable
  • Templates- WPF provides the facility to create reusable elements or “templates” that can be used in your application. There are two types of templates: control templates – these define how controls are displayed; and data templates – these define how data is rendered
  • Advanced binding – Apart from data binding, WPF applications allow command binding, key binding, event binding and many others
  • Styling – WPF provides very rich styling capabilities. Apart from regular things like margins, padding etc that are possible through regular stylesheets, you can also style nonvisual properties
  • Triggers – You can use the triggers provided by WPF to create animations and other flashy visual effects
  • Animation – Many animation features are available in WPF and you can integrate them with styles and triggers
  • 3D – There is some basic support for 3D graphics in WPF

Advantages and disadvantages of WPF
The main advantages of WPF are:

  • It unifies many concepts under a single roof
  • It provides intensive audio-video/multimedia capabilities
  • It makes collaboration between designers (they may use blend) and developers very easy by segregating code and design into separate elements
  • It makes complex graphical tasks simple including animations
  • It is capable of taking advantage of hardware acceleration when available
  • It has support for advanced typography (which was not there in windows forms)

The main disadvantages of WPF are:

  • It is limited to windows application development. If you want to deploy the application as a web application, you need to either create a WPF XBAP Application to run it inside the browser or use something else like Silverlight.
  • WPF is limited to .Net framework 3.0 or higher
  • It requires .Net framework to be installed and also some means to deploy the application

When should you use WPF
WPF is suitable for your application if:

  • You need heavy graphical elements in your application
  • Your designers and developers need to work in parallel
  • You have high end hardware that support directx

When should you NOT use WPF
There are certain scenarios when it may not be wise to use WPF. After all, no technology is suitable for all situations. WPF may not be suitable for your application if:

  • Your hardware is very low end
  • Your application is not very graphics intensive
  • If you do not or cannot have .Net framework 3.0

Tools for WPF
To start developing applications on WPF, all you need is Visual Studio 2008 or above. You can download the express edition of latest version i.e Visual Studio 2012 from here: Visual Studio 2012 Express

If you are a designer, you may wish to explore Microsoft Blend. It is targetted at designers and provides many advanced design features like drawing tools, animation timelines, palettes etc. You can download Blend for Visual Studio 2012 from here: Expression Blend for Visual Studio 2012

There are also other third party tools and plug-ins like Aurora, Expression Design, Kaxaml etc that make designing WPF applications easy in different ways. However, all these will not be explored in these tutorials. You are encouraged to try them out once you become familiar with the development of WPF applications.

WPF versus Silverlight
As we mentioned before, WPF is for desktop applications; To create web applications, you can either create a WPF XBAP Application to run it inside the browser or you can use silverlight. Silverlight is similar to WPF from a developer’s point of view (although they are different products) but it is meant for the development of rich internet applications. Silverlight applications are hosted on browsers (so the runtime is not CLR), and similar to Adobe Flash, it has a very small download and install footprint. So WPF is competitor for Adobe Air and Silverlight for Adobe Flash.

Like WPF, silverlight uses XAML for UI. However, the parsing of XAML is handled differently, particularly for applications developed on Silverlight 3.0 and below (the latest version as of now is 5.0). The XAML parsers for version 4.0 and above are somewhat similar to WPF parsers although there are some differences. For instance, the compiler has restrictions on the use of default namespaces. Only two namespaces can be used as default:

  • http://schemas.microsoft.com/winfx/2006/xaml/presentation

  • http://schemas.microsoft.com/client/2007

Note: This applies, however, only to XAML markup compile from the Page build action. For loose XAML such as the input sent to Xaml reader methods, it is possible to give any or no namespace as default.

In addition to this, some other differences include:

  • There are certain mapping restrictions between namespace and assemblies
  • There are certain constructs that are supported by WPF but are ignored in silverlight (such as x:Array, x:Code, x:Type etc). Many constructs are either not permitted or are ignored by the parser. The only constructs supported by silverlight are x:Null, x:Name, x:Key, and x:Class. It is however, possible to mimic the behavior of some constructs in other ways but more of that later
  • Certain properties like VisualTree are present in WPF but not in Silverlight
  • You cannot set the Byte properties of the “Color” structure in silverlight because it does not support native type-conversion of byte

For more details on parsing differences, refer to http://msdn.microsoft.com/en-us/library/cc917841(v=vs.95).aspx

Leave a Reply

Your email address will not be published. Required fields are marked *


nine − 7 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>