I hope to have an entry for each step in the process and you can feel free to jump in and out as you wish. Once you have the basics, you might not care about adding MVVM but it’s here for you should you need it.
Updating WinForms with WPF/XAML
- Getting Started
- Adding MVVM
- Adding Commands
- Messaging between Technologies
- XAML Islands – Where we want to be!
XAML IslandsMicrosoft recently announced XAML Islands at Microsoft Build 2018. This nifty technology lets you essentially embed UWP XAML inside WinForms and WPF applications. This is really wonderful technology that lets you bring Fluent Design language to WinForms and WPF. It lets you bring design patterns like MVVM to WinForms. This is truly awesome stuff but there is one caveat. Your new updated app with XAML Islands will only install and run on systems running Windows 10 with .NET Core 3. This is totally understandable as you simply cannot add Fluent design to a WinForms application that is primarily being used in a corporate Windows 7/8 environment or even a Windows 10 environment that hasn’t been brought forward by your IT department yet due to operating system limitations.
But there is a short term solution that will get you on the right track in WinForms. It will let you start creating XAML pages that have full separation of UI and code using design patterns like MVVM with little or no trouble. Once you have these pieces in place it becomes trivial to move forward to a fully Fluent Design when your Windows Platform catches up with your code.
Adding WPF/XAMLIn this blog I’m going to discuss how adding WPF controls to your existing WinForms application can be achieved and what some of the benefits you can gain will be. Not only will your UI gain a bit more of a modern look and feel but the relationships of your data with your views will be more separated yet work together with so much less code.
I have a customer with a legacy WinForms (VB/C# mix) application that needed updating. The code had been in development for close to a decade and was becoming increasingly difficult to maintain. When we discovered that one VB.net WinForm needed to be re-engineered, we wanted to implement it using a more modern WPF (XAML)/MVVM pattern. There were several reasons behind that preference:
First, they want to eventually move the application to Windows 10/UWP but it’s such a complicated application that moving it all would be prohibitively complicated and expensive. However the one form that has to be done can be replaced now.
Second, most, if not all, of their desktop systems are now running Windows 10 so we are not restricted in what we do by older operating systems. I do want to stress that “most, not all” which prohibits us from using XAML Islands which are in preview for insiders as of this writing and not practical yet in a production environment.
Lastly, while the preference was to jump to UWP, the XAML Islands only supports a very few number of controls today (with more on their way) and is still in “Developer Preview”. We might make use of them if they are relevant but the four controls currently in preview, the WebView, InkCanvas, InkToolbar and MediaPlayerElement were not something we needed for this particular application. (although the Ink controls might be soon, once the hardware investment in ink enabled screens happens).
So WPF it was. Adding WPF to a Windows Form is surprisingly simple. The big thing is to carefully consider what exactly you are trying to accomplish. So lets list our the goals.
- Use XAML to define the UX so it has a more modern feel and can be moved forward to UWP XAML without too much pain.
- Separate XAML from the Business logic that allows the form to do what it needs to do and providing a simple way to move that business logic forward to UWP without having to completely rewrite everything.
- Open Visual Studio 2017 (I’m using version 15.8.3).
- Click on New Project.
- Select Visual C#/Windows Desktop/Windows Forms App. (Up to this point you could and quite likely will use your own existing WinForms application)
- Inside your Solution Explorer add the following folders:
- View Models
- Inside Views folder, add a New Item
- In the tree, select Visual C# Items/WPF/User Control (WPF)
- Essentially we are going to create a user control in WPF that does everything we want it to and embed that in a WinForm using the included ElementHost control
- So, add a simple TextBlock and good ‘ol “Hello World” to your control.
- Build the Application. This will cause the control to appear in the “Toolbox” so you can add it to your WinForm.
- Open the WinForm
- Open the “Toolbox” and find your WPF control, probably called “UserControl1” unless you renamed it and drag and drop onto your form.
- Dock the control and you now have a WPF UX inside your WinForms App.
Of course, this just adds a really simple WPF form and you can go ahead and modify the XAML anyway you like. Make sure you make it responsive (no fixed height or width if you can avoid it) so that it resizes with your WinForm. Basically best practices for creating a WPF UX that is fully responsive is the way to go.
For those of you who are WPF developers you are off to the races. In our next blog I’ll show you how to add the MVVM pattern.
Next –> Updating WinForms with WPF… Adding MVVM.