A newbies guide to Windows Presentation Foundation

DevToolsGuy / Tuesday, April 7, 2015

Windows Presentation Foundation (WPF) is a Microsoft technology that was initially released as part of the .NET Framework 3.0. Previously known as "Avalon", WPF is used as a presentation system for building Windows client applications with a rich user experience. With WPF, developers can create a wide range of both standalone and browser hosted applications.

While it is useful, it is not universally known in the development community. In this post we will therefore look in more detail at this technology and understand how it can be valuable for developers.

Get to know WPF

At its core, WPF is a rendering engine that is resolution independent and vector-based. It is built to take advantage of the modern graphics hardware that is available in most business devices nowadays. WPF extends the core with a comprehensive set of application-development features such as:

 

  • Extensible Application Markup Language (XAML)
  • Controls
  • Data binding
  • Layout
  • 2-D and 3-D graphics
  • Animation
  • Styles
  • Templates
  • Documents
  • Media, text, and typography.

 

WPF is included in the Microsoft .NET Framework, so you can build applications that incorporate other elements of the .NET Framework class library.

Developers with an ASP.NET background will feel familiar with the ability to develop an application using markup and code-behind. WPF applications use the Extensible Application Markup Language (XAML) to implement the appearance of an application while using a managed programming language like C# or VB.NET, to implement its behaviour. Using this approach brings you the following benefits:

  • Development and application maintenance costs can be reduced, due to the fact the user interface is not tightly coupled with the code-behind.
  • Developers can work simultaneously with designers, because of the separation of the markup and the code.
  • There are multiple tools available for design and application code development such as Microsoft Expression Blend for design and Visual Studio which targets developers.
  • Globalization and localization is simplified.

A number of uses

The foundation of WPF is built on the .NET Framework, the System.Windows library and markup and code-behind. WPF can be used to build 2 types of applications:

Standalone applications

With Standalone applications you can use the System.Windows class library to create windows or dialog boxes with menu bars and toolbars. These applications can use all the other libraries in the .NET Framework too.

Browser-hosted applications

Browser-hosted applications, also known as XAML browser applications, will use the System.Windows.Controls.Page class and functions to create pages that use hyperlinks to navigate. Browser-hosted applications run in the browser, although note that these browser-hosted applications are executed in a partial-trusted security sandbox so they will have some restrictions. The type of restrictions can be compared with the restrictions on HTML-based applications. Still, the majority of WPF features can be safely executed from browser-based applications.

Core elements of WPF

WPF is built to use the rich graphic capabilities of modern devices and includes some core elements to develop extensive, scalable and flexible applications. The set of graphic features have the following benefits:

 

  • They are resolution and device independent. WPF is using a device independent pixel (1/96th of an inch). Each device independent pixel automatically scales to match the dots-per-inch (dpi) setting of the system it renders on it.
  • The WPF coordinate system is measured with double-precision floating-point numbers rather than single-precision.
  • WPF simplifies graphics programming by managing animation scenes for you; there is no need to worry about things like scene processing, rendering loops, or bilinear interpolation.
  • The WPF graphics system takes advantage of graphics hardware to minimize CPU usage on the device that it runs on.

 

WPF contains powerful graphic elements such as 2D and 3D shapes and geometries. These elements can not only be used for drawing but can also be used in an interactive way - such as in buttons. WPF can add 2D effects to elements like gradients, rotation, scaling and the engine can render 3D elements on the screen to build rich user interfaces.

Besides these powerful graphic elements, WPF supports animations like grow, shake, spin or fade to create page transitions or animate the elements in the application.

Best practices

Like with any other development platform there are many ways to achieve the desired result. To build the best WPF applications there are some recommended best practices for accessibility, globalization and localization and performance. The best practices can help developers building scalable and highly maintainable applications.

An option worth exploring

WPF is a powerful presentation technology built on the .NET Framework that enables the developers to create rich applications on a device-independent way. WPF is continuously evolving with each .NET Framework version. In .NET 4.5 there are some new features like a Ribbon control, improved performance when displaying large sets of data, synchronous and asynchronous validation of data, markup extensions for events and a lot more. With these new features WPF is a mature technology that is definitely worth a look.