Transitioning from Windows Forms to Wpf

Transitioning from Windows Forms to WPF

I like to blog about beginner articles for WPF, and there are a few in particular that may help you out:

  • Understanding the change in mindset when switching from WinForms to WPF
  • What is this "DataContext" you speak of?
  • A Simple MVVM Example

To summarize, the biggest difference between Winforms and WPF is that in WPF your data layer (the DataContext) is your application, while in Winforms your UI layer is your application.

To look at it another way, with WPF your application consists of the objects you create, and you use Templates and other UI objects to tell WPF how to draw your application components.

That's the opposite of WinForms where you build your application out of UI objects, and then supply them with the data needed.

Because of this, the designer isn't actually used that much since your application components are designed in code, and the designer is only needed to draw a user-friendly interface that reflects your data classes (typically Models and ViewModels)

And personally, I prefer to type all my XAML out by hand since it's faster and doesn't make as much of a mess as the drag/drop WPF designer does, although I do use the Designer on occasion to preview what my UI will look like.

So to your answer your question about if there's other WPF designers suited for WinForms developers, I would suggest that instead of looking for another designer, instead look to learn how to use WPF in the way it's meant to be used. Using WPF like it's WinForms means you miss out on much of what makes it so great :)

Migrating from Winforms to WPF

Should we go with Winforms or WPF?

This is a very broad question. But my answer to that would be this. Winforms designer is a painful experience and it is not as flexible as WPF by any means so if that is important to you then you should.

We want the design and structure to the exact same as what we have now.

Anything you do in Winforms you can replicate in WPF so no worries there.

What kind of pitfalls can I expect when migrating?

There are too many to name one of the most common ones for our migration was converting existing logic in the WinForms (yes it was a crappy, outsourced, legacy WinForms full of business logic) and connecting it to WPF elements. The process in and of itself is not that complicated but when you have too many intertwined pieces it can get pretty ugly.

Is there any place where I can lookup to see the process of migration?

The process of migration pretty much has to be iterative. So the way many people do it based on my research (articles, StackOverflow answers and such) is through ElementHosts.

1) Target a certain part of your WinForms that you would like to switch up to WPF and then take it out.

2) Replace it with ElementHost.

3) Then in that ElementHost you will have your newly rewritten WPF counterpart.

4) Test it out make sure that it works okay with the rest of the elements.

5) Go to step 1 until rest of the stuff on that window is replaced (You can do header, middle, footer or top, bottom any way you wish to go about replacing elements on the window it depends on your particular situation).

6) Once most of the stuff is replaced you can then combine all of those WPF User Controls into a WPF Window.

Convert a Winform application to WPF application

No you can't reuse code from winforms in WPF.

and even if you could, you shouldn't.

whatever you can do with XAML, you can also do without it

You should really use XAML to define the UI, and then use DataBinding and MVVM, which is a much more professional way of development than the traditional procedural winforms approach.

Not using XAML is much more troublesome than using it. It may look intimidating at first but it's a really awesome thing to work with.

Of course that you don't have any XAML when using Winforms

No, of course not. winforms is a really old technology that doesn't support anything. That's why they created the Visual Studio designer, otherwise no one would have ever used winforms for anything, because of the horrendous gargantuan behemoth amount of code required to do anything useful.

Can I use the same code for the WPF application that I used for the
winforms application and get the same result?

Probably, by adapting some class names and whatnot, but then you lose the main advantage provided by WPF, which is precisely getting rid of the horrible winforms-like code.

considering the past experience using Winforms, should I somehow
change the way I'm thinking about design and implementation that
worked for Winforms but are not that appropriate for WPF?

Yes. WPF supports MVVM, and that requires a really different mentality from the traditional winforms approach.

I strongly recommend reading Rachel's Excellent Post about upgrading from winforms to WPF.

Moving from WinForms to WPF

In general, WPF development is very different from WinForms. You should expect it will take some time to learn the new technology (or you might even need to hire new developers =)).

WPF approach is in many ways better than WinForms' one: check out styles and triggers, data binding, control templating, eventing model.

I would recommend you to start exploring it, but wait for the WPF 4 (and the boring MSDN page) to start the actual migration, because it is going to be even better and close some of the very annoying gaps.

Migrating from Windows Forms to WPF... was it worth it?

WPF is great. It is particularly good for extending controls like TreeView with customisations. You can add a string as an item in a TreeControl. You can also add a small panel containing an image and some text in various fonts and colours. Or you can add buttons, or anything you like. It has a completely general composability system. Same goes for ListBox, ComboBox, Button, etc. All their content or child items can be as simple as a string or as complex as a multipage document viewer with zoom buttons (if you want).

But the only way to find out is to try porting one of your forms. It shouldn't be too hard to make a WPF Window open from within your existing app. I started using WPF by making new GUI panels that were hosted inside a C++ Win32 application. Eventually it was so obvious that WPF was the way to go that we switched it around and made the outer shell WPF, with some ancient dialog boxes still implemented by the old C++ code where we couldn't be bothered to rewrite them (probably exactly what will happen with Visual Studio 2010).

One-Click application moving from Windows Forms to WPF

I think you actually SHOULD be able to deploy the WPF version as a new version of the Windows Forms application.

What version of the .NET Framework do each of the applications target?

Is the assembly name on both projects the same, and is the installation URL the same? (For ClickOnce, it has nothing to do with the projectGUID.)

WinForms to WPF - How do we get there from here?

We were in a similar situation and chose the following path: At the beginning we started to host a few WPF windows in the application shell (still WinForms). Of course there was some visible difference but we reduced the difference deliberately by toning down the new windows. We figured that by the time we would convert the remaining windows/controls it will be easier to "upgrade" to a more vivid experience since the UI will be entirely WPF and we can involve a graphical designer to work their magic based on XAML.

We have now reached the point where the majority of the windows are WPF. We have started the process of converting the WinForms shell app into a WPF based shell application hosting the remaining WinForms. We sill have sort of dull colors but users have started to notice the difference and although it is small our users still like the incremental positive change. Not too long and we will retire the last WinForm. That will be the point when we let our graphical designers off the leash!

As to performance: I can certainly not make a general statement as it heavily depends on your particular controls/windows. In our product (several hundred windows) we haven't found any significant performance issue related to the mix of WPF and WinForms.

We didn't look into any of the frameworks, so I'm afraid I cannot comment on those.

WPF in Windows Forms

If you find yourself in need to open a WPF Window from a WinForms program:

  1. Create/Add a new project of type "WPF Custom Control Library"
  2. Add a new Item of type "Window (WPF)"
  3. Do your thing with the WPF Window
  4. From your WinForms app, create and open the WPF Window.

code snippet:

WPFWindow.Window1 wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();

However ensure you have the following using statements:

using System; //Given 
using System.Windows.Forms; //Given
using System.Windows.Forms.Integration; //Not so Given.

Refer:

How to programmatically create a WPF window in a WinForm application

Open a WPF Window from WinForms & link form app with WPF app

How to add a WPF window to a WinForms App



Related Topics



Leave a reply



Submit