tags:

views:

347

answers:

6

This question is a result of a lunch-time conversation with a co-worker...I've read questions like WPF vs. Winforms...and I personally think that long term WPF is the way to go. The problem/question is what to do in the meantime.

Yes, WPF certainly has its advantages; not being built on GDI/USER is one of them. But at this point in time (i.e., late 2009; using VS2008 or perhaps even VS2005; Silverlight3 recently released and not yet widely adopted/deployed), WPF almost looks like it might be an "over engineered" solution. While I'm sure that will change with time, it doesn't make things any easier today, nor for the immediate (say <36 mo) future.

And let's face it, WinForms is really simple and easy; especially for the many co-workers who are still "happily" using MFC. Yes, it may be difficult to do slick animation, 3D graphics, gradients, etc.; but it's a very utilitarian solution that a lot of people (i.e., C++/MFC developers) readily understand today.

With that longish introduction--has anybody thought/tired/etc. about the idea of implementing (most of) WinForms using WPF (i.e., WinForms sans GDI/USER)? I'm sure given things like Control.Handle, 100% re-implementation isn't possible. But it certainly seems like many WinForms controls could be re-implemented using WPF "under the hood". Or is this really border-line impossible?

By "re-implement", I envision removing assembly references to System.Windows.Forms replacing them with (say) Microsoft.Wpf.WinForms and then rebuilding my application. After that I would expect to fix some (relatively small number of) compiler and/or runtime errors (say P/Invokes to Win32 APIs).

Something like this seems like a nice complement to Microsoft's various WinForms/WPF interop strategies such as WindowsFormsHost. For example, developers could start to use/learn WPF in a much more incremental fashion.

Edit: While the various "why?" discussions are interesting, they don't answer the basic technical question: "yes., ..here's how....; or no, ...because...".

A: 

A thing called the WindowsFormsHost can help out with moving controls over initially. However, I've found it to be pretty slow. You're much better off spending the time to learn WPF and write everything from scratch and take advantage of the features, especially data binding!

mrnye
Things like `WindowsFormsHost` is what I mean with "Microsoft's various ... interop strategies".
Dan
<3 WPF databinding
Nathan W
+3  A: 

First off, WPF isn't "over engineered". I would say it is just perfectly engineered, as it is an extremely complex endevor to implement a UI framework that supports rich composability, rich databinding, 2D and 3D in a single platform.

WPF has a slight learning curve, but it isn't very steep, and occurs in stages. Give yourself 2 days with it, and you will NEVER look back. WinForms is easy for easy things. WPF is easy for just about anything, and the capabilities you have via its extensive composability and databinding will put WinForms to shame for anything more complex than your most basic app.

As for WinForms controls being reimplemented...there is little need. A lot of the highly complex WinForms controls that are available from third parties are only required because of how difficult it is to customize the UI of any WinForms control. With WPF, enhancing any OOB control is easy, and nearly limitless.

I highly recommend WPF as the UI platform of choice, unless your target hardware is incapable of rendering WPF.

jrista
Notice my careful wording: "... [today] ... WPF almost looks like an 'over engineered' solution. While I'm sure that will change with time ...". I've spend a few days with WPF; I've got nothing against it. It just doesn't "play nice" with all of my (and co-workers) WinForms code/experience.
Dan
Today, yesterday, or tomorrow, it is still not an over engineered solution. If you have co-workers who arn't capable of grasping it, then by all means, keep using WinForms. However, if you think they are capable of grasping it, and are just fighting it because its new, then I think avoiding it is a poor decision. Progress requires pain at times, if only to have an even easier time than before once the pain is endured.
jrista
+4  A: 

I'm not really sure I understand your question - it is already possible to use WPF libraries in a Winforms like fashion (assemble everything in code with no XAML), and most of the common Winforms controls have their direct WPF analogs.

The real question is why would you want to? Many of the big challenges of developing Winform applications are easily solved with WPF databinding and templating techniques.

And I'm not even talking about fancy visual effects, which just take waaaaaaaay too much work in winforms, but simply something like visualizing a list of arbitrary items in a collection.

Furthermore, if you assemble everything in code Winforms style you don't get the design time editing support.

Egor
Call it Winforms sans GDI/USER: WinForms as it more-or-less exists today (including VS designer support), but built on WPF instead of GDI.
Dan
Without xaml, using wpf is almost the same as using winforms - and if someone were to go through all the trouble of creating a designer add-on for Visual Studio you could probably use it this way. Just like in winforms, you could have one massive designer generated function that creates and positions the child controls. But seriously, that is so the wrong way to go. I understand it's hard to start thinking in WPF mode, it wasn't an easy change for me as well. But it dramatically pays, off, and in way less than 36 months.
Egor
My first WPF apps were basically winform apps - with the xaml part of a control essentially acting like a the old .designer.cs file, and everything else done in the code behind file. So it's quite possible to use WPF in a winform like fashion already. Of course, once I understood datatemplates, controltemplates, wpf-style databinding, and the benefits of MVVM, doing things the old way very quickly became nearly intolerable.
Egor
It's not just about me "... thinking in WPF mode ...", it about all my co-workers too. Many of whom are still "happily" coding in MFC.
Dan
Oh I understand of course. Switching platforms is never an easy process, and only gets harder when the organization is larger. So yeah, if a switchover is extremely difficult it might not be worth it for your current needs. In this case, I think you're better off simply continuing to use Winforms - this is in no way an irrational decision. But if you'd like to use WPF you should probably spend some time learning the platform, as a whole organization.
Egor
And I think a WPF implementation of WinForms (as silly as that may sound) would make the overall transition to WPF easier. That would allow us to continue to "use" WinForms while more easily taking more advantage of WPF where there are obvious benefits.
Dan
"Most of the common Winforms controls have their direct WPF analogs." Exactly. If you want Windows Forms without GDI/User, as far as I can tell, you already have it. You might need to pick up some third-party controls, but the gaps are rapidly filling in. And nobody forces you to use the non-"utilitarian" features of WPF. Dan, could you say more specifically what is the gap you are trying to close? Is it designer support?
itowlson
@itowlson: I've made and edit to my question. I'd like to "use WPF" with minimal changes to my existing WinForms code base.
Dan
@Dan: "And I think a WPF implementation of WinForms (as silly as that may sound) would make the overall transition to WPF easier". I do not believe that would be a good idea. Just think if you had to place all of that back in XAML in the future...
Partial
Thanks Dan, I understand a lot better now. You're talking about (among other things) an adapter layer to iron out some of the properties and methods that got renamed in WPF. I can definitely see where you're coming from on that one!
itowlson
Well, aside from having a VS designer that generates the control in CS code (you're pretty unlikely to ever get this), what kind of scenarios were you thinking of? Typically when I make winforms apps the process kind of goes like this : throw up some controls in VS designer, wire them up to event handlers, add business logic, etc. You can the exact same thing with WPF and the code behind file will look almost the same. And you can do all those fun winform things like "iterate through a collection and add a checkbox for every one". What kind of trouble did you run into when you guys tried it?
Egor
@Egor: we've got WinForms infrastructure like custom dialogs, location/size persistance, fonts, etc. Not to mention, a new WPF dialog (created in designer) does not look **EXACTLY** the same thing done in WinForms. It's hard enough already making the WinForms dialogs looks like MFC ones.
Dan
Ok now that I read your edit I think I understand what you'd like to see. I'm not sure if the challenge of creating something like that is really worth it. Seems like you inevitably end up with some bizarre hybrid of both platforms in trying to reconcile the fundamental differences of the two. Hosting Winfom controls in a WPF app and vice versa seems like a decent compromise to me.
Egor
Porting apps from one platform to another is never easy, and it sounds like you have a lot of investment in Winforms and MFC already. Honestly, trying to cram WPF in there as well sounds like a bad idea. As for the dialog, the default templates for buttons etc should look pretty similar to standard Windows themes, but if they don't you can create templates that do. But really what it boils down to is that porting a large hybrid Winforms/MFC app to WPF (or anything else) is just not a simple easy process.
Egor
@Dan: Why did your company keep MFC in the first place? The longer you wait to port towards a new technology the more money it will cost!
Partial
A: 

You really must look at it from the long term perspective of the life of the application you are creating. While WPF might not be as mature as Windows Forms, it allows a programmer to separate design (XAML) and logic (C#/VB). I do not believe it would be good if you create your controls in WPF in a Windows Forms style. Imagine placing it all back in XAML for some reason in the future!

That being said, if you really wish to bring some Windows Forms' controls into WPF it is entirely possible. You simply need to add a reference to WindowsFormsIntegration, add the appropriate namespaces for it in your code and a add a WindowsFormsHost tag in your XAML. In my opinion, using this should only be temporary (it is rather slow for the moment). On the other hand, future frameworks will most likely make WPF a lot better (having 4.0 in my mind).

Learning WPF now is probably the best thing you can do. Getting to know your way around and using the WPF Toolkit for certain missing controls from Windows Forms can be a bit time consuming at first since there are some things that are different from Windows Forms (control customizing and data binding comes to mind).

If you keep on using Windows Forms you will be missing out on new features in WPF that make development faster. For example, you can have people working on the design while others work on the logic side of the application because of the more pronounced separation of design and logic.

Partial
"... I personally think that *long term* WPF is the way to go ..."
Dan
Exactly, so starting now would only give you an edge... I believe that in one way or another people will have to change and use modern technologies. WPF is in a sense a normal continuation to Windows Forms like Windows 7 is to Windows Vista and Windows XP.
Partial
*You* try convincing 100s of long-time C++/MFC developers they should start using WPF today.
Dan
@Dan Just come to work one day with a plastic gun on your hip and say "we are using WPF onwards from today, anyone have a problem with this". Making sure of course the gun is clearly visible ;)
Nathan W
C++/MFC is a painstaking and slow way of creating an interface. Certain languages are better for certain tasks and while others are not. For sure C++/MFC allows more efficient code than what can be done in .NET languages but it is really time consuming! While your company take a year to create this super fast application with C++/MFC another (perhaps a rival company) is creating 5 applications by using WPF and making more money because of that.
Partial
For sure C++/MFC is not obsolete but the costs are simply too much for what can be done with other technologies that are currently available. Also, with the computers of today an application made in C#/VB, even if it is not as efficient as one that is made in C++, will clearly run without any problems.
Partial
Precisely, the way to show MFC programmers that it's time to switch is to demonstrate that many problems that require whole weeks to solve with MFC can be literally done in minutes with WPF. MFC is very efficient, true, but also incredibly labor intensive both in creation and maintenance. Of course... the problem is that after the switch to WPF you might find that you no longer need 100s of programmers to handle the workload.
Egor
Not to mention that a C++ programmer cost a whole lot more than a .NET programmer... but that is another story.
Partial
Do they? Never knew that haha, maybe it's time to respec my skills. I use many languages in my job, C++ included, but I've successfully stayed away from MFC completely. Of course, now is probably not the time to invest effort in learning it, seems like it's doomed to be a legacy platform from here on out.
Egor
I mean a well qualified C++ programmers normally are because C++ requires a lot more job and sometimes more knowledge on certain aspects. Try connecting to a database in C++ compared to how easy it is in C#/VB. :P
Partial
Well unmanaged C++ that is...
Partial
+3  A: 

Honestly, I think you could probably implement a layer to allow you to port most of WinForms to WPF, but you really shouldn't. Let me use an analogy - Moving from WinForms to WPF is like moving from an imperative language to an object oriented one (or from OO to pure functional). It's really hard, nothing seems to work right, solutions don't make sense, everything seems over-engineered, and then... you get it. You have a flash of insight, the light bulb goes on and you see a whole new world where tasks are easier to accomplish and easier to maintain (than in WinForms).

But first you have to grok it. It's really easy to keep coding in the WinForms style in WPF so you have to force yourself not to or you'll never take that next step. Force yourself to start from a clean slate, learn how to apply MVVM, take a look at Prism, build some really simple apps and then add to them to explore how to manage complexity the WPF way. You might implement WinForms in WPF but all you would ever have is people coding WinForms in WPF. You basically end up with the equivalent of (from the Imperative -> OO days) making every class a singleton with all static methods. You might be "using" WPF, but you (and/or your coworkers) will never grow past that stage.

From what I can glean from your situation I would recommend that you not try to switch to WPF unless you are writing a new application with a small team. If that does end up being the case then the small team can be a nucleus for change because once one person in your organization "gets it" they will teach others near them until the light bulbs start turning on all around over the place. That's what we've done in my current organization and looking back the progress everyone's made just in the last 6 months is staggering.

Bryan Anderson
Honestly, that's a great analogy actually. I totally remember having a lightbulb moment that you're talking about, and I remember my struggles prior. And I also second your recommendation - Winforms aren't going anywhere anytime soon, there is no need to port a complex already hybridized app to WPF without a really compelling reason.
Egor
As the only answer that said "... you could probably...port most of WinForms to WPF..." it gets marked as "correct" (need to preserve my 100% accept rate. :-) ).
Dan
+1  A: 

An analogy for your coworkers:

Which is better, the automobile or the horse and buggy?

  1. Automobiles are not as mature a technology as the horse and buggy. Horse and buggy has been around over 2000 years, automobiles less than 200.

  2. Automobiles are overly engineered and extremely complex. Horse and buggy is simple.

  3. People in many parts of the world don't know how to drive an automobile but have good skills with the horse and buggy.

Question: Can we simplify our automobiles so they are controlled by reins, eat hay, and be repaired by veterinarians and carriage-makers?

My point here is that WPF is a very mature technology by now, much more powerful and easy to use than WinForms, and isn't at all difficult to learn. It should be the vehicle of choice except for "country drives" and other specialized purposes.

I agree with the others that it is trivial to use WPF in a WinForms-like manner so there need be no significant learning curve if you want to use it that way, but it is wasteful not to take advantage of the new paradigms.

I disagree that you should delay implementing WPF in your main application. It's very easy and painless to mix WinForms and WPF content in a single application. I recommend building new pieces in WPF and leaving old pieces in WinForms until there is a need to touch something. Even if your existing application is native C++, it is still easy to use WPF with it as long as the data is easily accessible from managed code.

Ray Burns
Last week I added a super simple dialog. "Had" to do it in WinForms because we've got a bunch of integration stuff (fonts, persistence, help) in WinForms. Engineers (including me) always want to "re-write and do it better", but there's little business case for throwing away working code.
Dan
And what would you have done in 1900 with $500 to spend on transportation? Buy this new horseless carriage thingy that is noisy, complex, a breaks down all the time? Or get another horse and some hay? Especially since you already have a saddle, stable, and a blacksmith down the road; and everyone else in town rides horses.
Dan
In 1900, cars was quite primitive. WPF is a very mature technology, comparable to the cars of today. What you are missing is infrastructure in your locality. A much better comparison would be deciding whether to buy a car or a horse-and-buggy today in a third world country. Of course the few weeks it takes to build a WPF infrastructure is far, far cheaper than the gas stations, auto repair shops, etc, that would be needed to make cars convenient to use in a remote village in Bangladesh.It is hard to know what to do with your saddle and your leftover hay, though. :-)
Ray Burns
On a more serious note: I had the same situation with lots of WinForms integration stuff. It took me about three weeks to make it all work with WPF as well. I created a clean WPF API for each bit of functionality, then wrote shims to allow the WinForms code to use the new API without any code changes. The time I spent was easily paid back within a month through the greater productivity of WPF.
Ray Burns
Taking *n* weeks (or *m* months, for small *n* and/or *m*) is only part of the problem. Even if I did this infrastucture re-coding "on my own time", there is still the fear/danger of breaking something. The WinForms code is written, debugged, and working; is it asking too much to have a wider array of transition options?
Dan
@Dan: This is why in the mean time you use that Winforms/MFC you have for what ever you need it for and during that time you develop based on what is working right now and make it in WPF. You don't stop something that is working for something that is in development and testing! Developing in WPF takes less time than Winforms and a lot less time than MFC.
Partial