[STAThread]
static void Main()
{
var wpfApp = new WpfApp();
Uri resourceLocater = new System.Uri("/AssemblyName;component/app.xaml", System.UriKind.Relative);
System.Windows.Application.LoadComponent(wpfApp, resourceLocater);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Hosting MVVMLight WPF app in WindowsForms
-
21-06-2023 - |
Question
Is it possible to host an MVVMLight WPF application's windows in Windows Forms? What would be the proper bootstrapping procedure?
UPDATE:
By looking at what's inside InitializeComponent of App.xaml, I was able to bootstrap MVVMLight like so:
in Program.cs
[STAThread]
static void Main()
{
var wpfApp = new WpfApp();
Uri resourceLocater = new System.Uri("/AssemblyName;component/app.xaml", System.UriKind.Relative);
System.Windows.Application.LoadComponent(wpfApp, resourceLocater);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Then, in my on-click event handler, to show MainControl WPF UserControl I can use:
var window = new MainControl();
var form = new Form(){MdiParent = this};
form.Controls.Add( new ElementHost(){Dock = DockStyle.Fill, Child = window } );
form.Show();
If no one has better idea, I will accept this as an answer in a couple of days.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow