Following the accepted answer (good!), I would like to show you how to implement WinStorms Bootstrapper in the first ViewModel, such that:
- You will not need to create a WPF window and
- You do not need to directly contact the ViewModel from the view.
To do this, we need to create our own version of WindowManager, make sure that we do not call the Show method in the window (if applicable to your case) and enable the binding.
Here is the complete code:
public class WinformsCaliburnBootstrapper<TViewModel> : BootstrapperBase where TViewModel : class { private UserControl rootView; public WinformsCaliburnBootstrapper(ElementHost host) : base(false) { this.rootView = new UserControl(); rootView.Loaded += rootView_Loaded; host.Child = this.rootView; Start(); } void rootView_Loaded(object sender, RoutedEventArgs e) { DisplayRootViewFor<TViewModel>(); } protected override object GetInstance(Type service, string key) { if (service == typeof(IWindowManager)) { service = typeof(UserControlWindowManager<TViewModel>); return new UserControlWindowManager<TViewModel>(rootView); } return Activator.CreateInstance(service); } private class UserControlWindowManager<TViewModel> : WindowManager where TViewModel : class { UserControl rootView; public UserControlWindowManager(UserControl rootView) { this.rootView = rootView; } protected override Window CreateWindow(object rootModel, bool isDialog, object context, IDictionary<string, object> settings) { if (isDialog)
I hope this helps someone with the same needs. It certainly saved me.
source share