Converting WinForms to WPF: Should you consider it?

DevToolsGuy / Thursday, April 30, 2015

WinForms has long been a popular way to develop form and GUI based applications, generally for Windows based devices. Part of the .NET framework, and originally seen as a replacement for Microsoft Foundation Class Library (MFC), WinForms applications are ‘event driven’. That is they sit and wait for a user to interact with them, before springing into action and reacting to this input.

Whilst still a useful technology for many (we offer a whole range of useful and popular controls) WinForms is officially a technology in ‘maintenance mode’. According to Microsoft, the product is no longer subject to active development, won’t be seeing any new features in the future, but will benefit from bug fixes.

With that in mind, what is the argument for converting a WinForms app to a more modern technology like Windows Presentation Foundation (WPF)? In this post we will take a look at this topic, weighing up the various options and pros and cons.

Windows Presentation Foundation

Windows Presentation Foundation is a different API for building GUIs. Separate to WinForms, but also included in the .NET Framework, it is based on DirectX and uses a language called XAML to describe its components and elements. Many developers often ask the question: “Can I easily convert my WinForms app?” Let us look at some useful answer and pointers:

1. A chance to improve your code

After deciding to ‘convert’ your WinForms app to WPF, take a step back and decide if the best approach isn’t to simply rewrite your code from scratch. Undoubtedly you will learn a lot during the process, and the end result will most likely be altogether better code. Switching from WinForms to WPF does involve a change in mindset, this article looks at how your approach should differ. WPF is at its core very different to WinForms, understanding the basics of WPF before you start to migrate any code is a vital step.

2. Converting C#.NET WinForms designer code

If you are looking for a more ‘automated’ approach, there are utilities that will convert C#.NET designer code to XAML. This one is good example, though we do advise have a good manual check over the code as well. This CodePlex project is also capable of handling basic conversion, taking basic WinForms Form Controls and even carrying over event handling code. Beware the project hasn’t been updated for a number of years. A more up to date option can be found here, and this tool does come with support for a number of different controls.

3. Hosting WinForms inside a WPF application

Rather than converting an entire application, it is possible to reuse WinForms code by hosting controls inside a WPF application. Microsoft offers a very detailed walkthrough on hosting WinForms composite controls inside a WPF application.

4. Validating the results

If you are new to WPF, or converting/porting code over from WinForms, then it is a good idea to double check the final results. Some useful tools to do this include:

 

  • Last updated a few years ago, Snoop is however still a useful way to visually debug WPF apps.
  • Shazzam helps to testing WPF pixel shaders
  • Any good .NET developer will know about Reflector
  • Microsoft Blend started life as a stand-alone user interface tool, but is now part of Visual Studio. Either way it is an interesting way of designing WPF apps, and can be a useful aid for those moving from WinForms.
  • The WPF Performance Suite helps to analyze the behaviour of WPF apps, and provides a means to implement improvements. Useful for those starting out with WPF or refactoring code form WinForms apps.

 

Multiple options

As experienced developers will know, the .NET framework offers a number of methods and solutions to solving any given problem. When it comes to GUI applications WinForms and WPF are both still very much valid choices, and as we have seen in this post there are means to convert between the two approaches. Whatever the route taken, Infragistics offers a range of pre-built UI components to help make the final results even more impressive.