Wpf 应用程序关闭问题
在一个prism应用程序中,我创建了一个从模块开始的登录表单。用户通过身份验证后,登录表单wait for all application modules(等待所有应用程序模块)已初始化(如splah屏幕显示有关加载模块的一些信息),然后启动shell。除了用户单击登录表单的“取消”按钮外,所有操作似乎都正常工作。我希望应用程序关闭,但由于登录表单在另一个线程中启动,我无法使用application.Current.shutdown()(仅限于work Environment.Exit(0),但我不确定这是否正确)。我尝试从Application.Current.Dispatcher调用,但应用程序仍在运行。 这是登录模块初始化,在这里我捕获了应该关闭应用程序的事件:Wpf 应用程序关闭问题,wpf,mvvm,prism,Wpf,Mvvm,Prism,在一个prism应用程序中,我创建了一个从模块开始的登录表单。用户通过身份验证后,登录表单wait for all application modules(等待所有应用程序模块)已初始化(如splah屏幕显示有关加载模块的一些信息),然后启动shell。除了用户单击登录表单的“取消”按钮外,所有操作似乎都正常工作。我希望应用程序关闭,但由于登录表单在另一个线程中启动,我无法使用application.Current.shutdown()(仅限于work Environment.Exit(0),但
public void Initialize()
{
Dispatcher.CurrentDispatcher.BeginInvoke(
(Action) (() =>
{
Shell.Show();
EventAggregator.GetEvent<LoginStatusEvent>().Publish(new LoginStatusEvent { LoginStatus = LoginViewStatus.Close });
}));
ThreadStart showSplash =
() =>
{
Dispatcher.CurrentDispatcher.BeginInvoke(
(Action) (() =>
{
Container.RegisterType<LoginViewModel, LoginViewModel>();
Container.RegisterType<LoginView, LoginView>();
var login = Container.Resolve<LoginView>();
EventAggregator.GetEvent<LoginStatusEvent>().Subscribe(e => login.Dispatcher.BeginInvoke(
(Action)(() =>
{
if (e.LoginStatus == LoginViewStatus.Aborted)
{
login.Close();
Application.Current.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
//Environment.Exit(0);
}
else if (e.LoginStatus == LoginViewStatus.Close)
{
login.Close();
}
else
{
WaitForCreation.Set();
}
})));
login.Show();
}));
Dispatcher.Run();
};
WaitForCreation = new AutoResetEvent(false);
var thread = new Thread(showSplash) {Name = "LoginView Thread", IsBackground = true};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
WaitForCreation.WaitOne();
}
public void Initialize()
{
Dispatcher.CurrentDispatcher.BeginInvoke(
(行动)(()=>
{
Shell.Show();
EventAggregator.GetEvent().Publish(新的LoginStatusEvent{LoginStatus=LoginViewStatus.Close});
}));
ThreadStart showSplash=
() =>
{
Dispatcher.CurrentDispatcher.BeginInvoke(
(行动)(()=>
{
Container.RegisterType();
Container.RegisterType();
var login=Container.Resolve();
EventAggregator.GetEvent().Subscribe(e=>login.Dispatcher.BeginInvoke(
(行动)(()=>
{
如果(e.LoginStatus==LoginViewStatus.Aborted)
{
login.Close();
Application.Current.Dispatcher.BeginInvoke((操作)(()=>Application.Current.Shutdown());
//环境。退出(0);
}
else if(e.LoginStatus==LoginViewStatus.Close)
{
login.Close();
}
其他的
{
WaitForCreation.Set();
}
})));
login.Show();
}));
Dispatcher.Run();
};
WaitForCreation=新的自动重置事件(false);
var-thread=newthread(showSplash){Name=“LoginView-thread”,IsBackground=true};
SetApartmentState(ApartmentState.STA);
thread.Start();
WaitForCreation.WaitOne();
}
感谢您的帮助 为什么您的登录UI在后台线程上运行?似乎登录UI应该在GUI线程上运行。然后您就有了一个BackgroundWorker对象(比上面的代码更容易使用,也更易于维护),它可以在ProgressChanged方法调用GUI线程时随时运行ProgressChanged来更新UI 我经常重载BackgroundWorker,使其类似于MyDataObjectBackgroundWorker,并创建一个保存对象的属性。通过这种方式,我可以在ProgressChanged中轻松地来回传递数据(比如加载的模块上的信息)