通过Instantiate App.xaml与MainWindow.xaml启动WPF应用程序

时间:2017-05-27 00:22:51

标签: c# wpf xaml

为了从Win Form App启动WPF应用程序,我发现了两种方法。一个是通过实例化App.xaml:

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    var t = new Thread(() =>
    {
        var app = new App();
        App.ResourceAssembly = app.GetType().Assembly;
        app.InitializeComponent();
        System.Windows.Threading.Dispatcher.Run();
    });

    t.SetApartmentState(ApartmentState.STA);
    t.Start();
}

另一个是实例化MainWindow.xaml。

private void button1_Click(object sender, RibbonControlEventArgs e)
{
   var t = new Thread(() =>
   {
       MainWindow window = new MainWindow();
       window.Closed += (s2, e2) => window.Dispatcher.InvokeShutdown();
       window.Show();
       System.Windows.Threading.Dispatcher.Run();
   });

   t.SetApartmentState(ApartmentState.STA);
   t.Start();
}   

请您帮我理解两种方法之间的区别是什么?

0 个答案:

没有答案