使用下面的代码,我能够生成第二个窗口,但是第二个窗口在初始化后立即关闭.我在这里做错了什么?
private void button_Click(object sender, RoutedEventArgs e)
{
Thread sta = new Thread(delegate ()
{
Window1 w = new Window1();
w.Show();
});
sta.SetApartmentState(ApartmentState.STA);
sta.Start();
}
解决方法:
没关系,我找到了!我没有放:
System.Windows.Threading.Dispatcher.Run();
在结束时.
该代码现在显示为:
private void button_Click(object sender, RoutedEventArgs e)
{
Thread sta = new Thread(delegate ()
{
Window1 w = new Window1();
w.Show();
System.Windows.Threading.Dispatcher.Run();
});
sta.SetApartmentState(ApartmentState.STA);
sta.Start();
}