0

I'm writing an application that will run with either WPF or WinForm. The idea is that if Windows Presentation Foundation isn't on a system (say an older server for example) I want WPF to failover to Winform.

I'm not looking to "combine" them so much as design a WPF form separately from the WinForm.

Is this possible?

mlw4428
  • 510
  • 1
  • 6
  • 21

4 Answers4

4

This doesn't make sense.

  • If develop a .Net Application targetting .Net Framework 3.0 or later, then WPF WILL be present (It's part of the .Net Framework).

  • If you don't have that version installed, a winforms application won't run either if it's targetted to a non-present framework version.

  • Any given .Net application must target a specific version of the framework, and of course that specific version must be present in the machine where it will run.

  • If you want some "fallback" mechanism, you will have to build 2 separate versions of the same application, one targetting .Net 2.0, and another targetting 3.0 or later, which then would be independent applications unrelated to each other.

Clarification:

If, say .Net Framework 4.0 in installed on a given machine, then WPF is installed on that machine. WPF is part of the .Net Framework starting at .Net 3.0.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Ah, I thought WPF was a Windows-level (running at a level below .NET) framework that was installed separately. So you're saying it's built-into .NET and doesn't require additional installation? – mlw4428 Apr 17 '13 at 18:08
  • This answers the question. Thanks! – mlw4428 Apr 17 '13 at 18:13
0

I think this is not possible with very much work. Normally windows loads all dependencies on start into the memory if a dependency is missing your application will crash. There are options to let it load delayed but this is quiet hard work.

You could implement that with a dll for windows forms and a second one for WPF. You could check the system on startup and load than the GUI for the currient sytem.

However I would recomment just to write a Windows Forms UI. To implement every think double is not very effecient.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • I know with WinForms a lot of boilerplate code runs that initializes the form. Does WPF do this? I'm digging and it doesn't appear to be so, but there's got to be a way it does. With WinForm I've thought about bumping up in the boilerplate code and on load it checks to see if WPF is available or some sort of command line switch that then enables the WPF form. I"m just thinking out loud too -- fairly new to WPF.\ – mlw4428 Apr 17 '13 at 18:06
  • 1
    @user1693074 what you say doesn't make sense. Please read my answer. It's not about "WPF is installed or not" as if it were some add-on component, it's about .Net Framework versions. – Federico Berasategui Apr 17 '13 at 18:07
  • @HighCore I replied to your "main" reply and keep my replies there to keep this organized. Thanks! :) – mlw4428 Apr 17 '13 at 18:10
  • Actually, doesn't .NET load assemblies on-demand? A missing assembly won't necessarily cause an error if your program never calls a method (or uses a class) that uses that assembly. However it might be very tough to make a WPF-capable program that doesn't reference WPF in any way during the (WinForms) startup sequence. – Qwertie Apr 17 '13 at 18:19
  • WPF does also many stuff in the background especially the parsing your your xaml file which could cause Exceptions (which are very hard to handle). However WPF was intedruced with .net 3.0 so far has HighCore right. You could implement a .net 2.0 luncher which checks if .net 3.0 is aviable and loads your WPF GUI. The logic could be implemented in .net 2.0 so far you could share that libs. – rekire Apr 17 '13 at 18:19
  • @Qwertie AFIK is this true for interop with native dll files. But as far I remember all references must be sloved to be able to lunch. – rekire Apr 17 '13 at 18:21
0

Are you planning to have an installer for your application? If so you can check which version of the .NET framework is installed on the system and then determine which version of your application to install.

To reuse some of your code you then could have a library that both versions of your application use for the business logic that is unrelated to the presentation.

risehigh
  • 281
  • 1
  • 4
  • Or you could just have the application installer take care of installing the appropriate version of .NET for you (assuming the customer allows this). – Brandon Dybala Apr 17 '13 at 18:12
0

I have actually implemented a program that has both WinForms and WPF user interfaces. I implemented the WinForms first, and then added WPF when people complained that WinForms looked "too 90s". Currently the old UI is still available with a "-winforms" command-line option. I used MVVM with a strict rule against using WPF classes in the viewmodel; thus, the majority of the code is shared between the two versions. WinForms itself doesn't support the MVVM pattern very well, but I used the Update Controls framework which provides its own unique approach to MVVM, as a library that sits on top of WinForms (and integrates seamlessly with WPF).

However, what you want to do is fairly challenging. WPF comes with .NET 3.5 (3.0?), so you'd probably need separate executables, one for .NET 3.5 and one for 2.0. (I'm not 100% sure about this, since .NET 3.5 actually still uses the same 2.0 CLR binaries; so it might be possible to do it all in one executable, somehow.)

But why go to so much trouble? Almost all computers now have .NET 3.5, and those that don't can be upgraded.

Qwertie
  • 16,354
  • 20
  • 105
  • 148