Caliburn Micro in WinForms application

Apr 29, 2011 at 11:33 AM

I'm working on a hybrid application. The shell is WinForms and I'm trying to integrate some WPF parts. The goal is to use MVVM in a ViewModel first manner with convention based viewdetection and binding.

As the bootstrapper depends on System.Windows.Application.Current it's not possible to use it within a Winforms application.

Is there any way to get around this?

Apr 29, 2011 at 7:28 PM

I actually have the same problem myself. I have a *HACK* work around in place that lets me develop and testing UI for now (Until i actually want to figure out the real fix to the problem).

My temporary hack works by just creating a Application.xaml/cs file and directly calling Run() in the winForms application. Since there is no System.Windows.Application in the current app domain at the time, .Run() works. The below snippet is launched from a injected MenuItem on the parent .Net 2.0 winforms application. To redisplay the main window after it's closed i think you would need to  expose the bootstrapper in the application object and call DisplayRootView. I just havent tried this yet. I'm not sure how friendly this would be to other applications trying to use the same trick since the Application object is a singleton and only 1 per AppDomain.

 

          if (System.Windows.Application.Current == null)
          {
              MyApplication app = new MyApplication(); //The application defines the Caliburn framework normally. 
              app.Run();
          }
          else
          {
              //Do something else I haven't worked out yet. 
}

If you find a better/cleaner solution let me know I'm in the same boat. =)

Coordinator
May 2, 2011 at 3:02 PM

If one of you could add a ticket for this...and this is very important.....along with a sample that demonstrates the problem, I will see if I can work something out. No promises, since this isn't a core use case, but if you provide the sample, I'll give it a look.

May 2, 2011 at 5:06 PM

I'm sure I could create a sample app for this in the next day or two if CodeJude doesn't beat me to it. Thanks for offering to take a look!

John

May 2, 2011 at 5:56 PM

I just went ahead and created a sample project which meets my particular requirements. I'm not sure how CodeJude uses the framework and if his requirements are different than mine. So hopefully you can come up with a fairly flexible solution ;). I know how to solve it for my paticular problem, but it doesn't solve it in all instnaces.. specifically if someone else registers a System.Windows.Application object before me.

 

Again thanks!

 

Aug 4, 2011 at 3:36 PM

Hi.

I put a call to InitalizeComponent() in a ctor in my App.xaml.cs class and the bootstrapper did it's thing.  I shall keep you posted with any updates.