Have you ever struggled to create a button with an image and text in WinForms? WPF can easily help you out with that. Ever wondered how some Windows applications come in attractive layouts, colours, animation, that look so distinct from standard Windows UI? They might just have been created with WPF.
Windows Presentation Foundation (WPF) is a powerful framework to build Windows desktop applications with a lot of flexibility and separation between presentation and behaviour. As such, it is one of Microsoft’s approaches to the GUI framework and was first introduced in .NET Framework 3.0 (November 2006), but many features have been added since. Even though Microsoft desktop application development is now moving towards the new UWP (Universal Windows Platform), WPF is still popular, especially for building “traditional” desktop applications that work well and compatibly with the older Windows versions like Windows 7.
WinForms is an older technology preceding WPF, and it is an additional layer on top of standard Windows control. WPF differs from WinForms in that it has been built from scratch, so doesn’t rely on standard Windows control rules. To make designing tasks easier, the presentation (i.e. GUI layout and controls) is defined in a separated XAML file, although WPF doesn’t need to have XAML at all. Every control in WPF can act as a container, meaning we can include image and text control within a button control.
Why do we need WPF if Microsoft has provided WinForms? Some of the reasons WPF is superior to WinForms are:
– Free execution (XAML): we can write the UI layout once in the XML- based XAML file, and we can run it in a WPF browser application or as a desktop application.
– Control inside control: You can define a control inside another control.
– Bindings: Establish connections between multiple objects with simple settings.
– Common look & feel: Think of it like CSS classes/styles. We can set a style to controls in the layout, so they can have the same properties and look.
– Declarative programming: we can achieve certain actions in XAML, so we can express what we want with more code efficiency.
– Expression Blend and animation: WPF is capable of animation with the help of DirectX.
– Faster Execution (Hardware rendering): WinForms uses GDI, while WPF internally uses DirectX. DirectX is capable of hardware rendering. A GPU is specifically meant for rendering and displaying, and is generally much faster than using a CPU. WPF is smart enough to switch between software and hardware rendering or may use both depending on the system hardware environment. This can make application execution much faster.
– Graphic independency: WinForms uses pixels as the measurement unit. WPF uses DIP (Device independent pixels) where 1 DIP = 1/96th inch. If the monitor resolution changes, WinForms will change it in presentation view, while WPF will still maintain its intended presentation.
– Support for multi-touch input: This support is available on Windows 7 and above.
– Integrated media services: this feature makes it easier to develop applications with images, audio, and video elements.
Microsoft provides two main tools for developing WPF applications: Visual Studio and Expression Blend. Their feature sets are similar, but Expression Blend is intended more for designers. They are both equally powerful and very capable of building WPF applications.
While WPF boasts many advantages, it does have some disadvantages.
– Typically, a WPF application can take a little longer to design. WPF gives you the flexibility and customisation of the layout and look of the application, but sometimes getting the look that you want takes more time.
– There are still a few bugs found in WPF compared to the more mature and tested WinForms.
– WPF can have a steeper learning curve and fewer online resources and communities compared to WinForms.
– WPF does not feature the same multi-platform capabilities compared to other XAML-based competitors.
With that said, WPF is not an absolute necessity to build a windows application. WPF is a strong contender if you want to create a web-like desktop application which combines various media types, animations, and advanced graphics. WPF is the natural successor to WinForms and is a great tool to learn.
It is worth mentioning that there are several alternatives to WPF, such as Universal Windows Platform (UWP), Electron, and Xamarin. These products have their own pros & cons. For example, UWP desktop applications are only compatible with Windows 8.1 or 10, while Electron and Xamarin applications support more platforms.
Choosing the technology/framework that is best for you requires you to first answer at least these questions:
– What Operating System/s are you targeting?
– What programming language is your team already comfortable with?
– Is the framework easy enough for your team to use, including establishing a build environment? This can have a major impact on your budget and deadline.