我正在进行一个使用以下技术的项目:
4.0)
)
目前,我正在使用代理生成的开始/结束方法异步调用我们的Web服务之一。调用成功,客户端能够在工作线程上接收Web服务的响应。
一旦收到响应,我将继续引发一个事件。订阅事件的类继续使用PRISM请求UI导航:
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
this.RegionManager.RequestNavigate(RegionNames.LoginContentRegion, projectSelectionViewUri)));由于异步WCF响应没有在UI线程上捕获,所以我被迫使用Application.Current.Dispatcher.BeginInvoke(...).调用UI线程
这里的问题是调用似乎什么也不做。UI不更新,也不引发异常。
使用dispatcher尝试调用的类是View的视图模型。它使用控制反转(使用统一容器)创建。
下面是视图的构造函数,它请求视图-模型:
public CredentialsInputView(ICredentialsInputViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
...
}前面的代码将导致调用视图模型的构造函数。我尝试将dispatcher存储在VM的构造函数调用中,但使用它稍后调用UI导航似乎没有帮助。我认为视图模型不是在UI线程上创建的:
private static System.Windows.Threading.Dispatcher dispatcher;
/// <summary>
/// Initializes a new instance of the <see cref="CredentialsInputViewModel"/> class.
public CredentialsInputViewModel(ICodexLoginService codexLoginService, ISessionService sessionService, IRegionManager regionManager)
{
dispatcher = Application.Current.Dispatcher;
...
}如何从在工作线程上引发的事件中调用UI线程?
发布于 2012-01-09 23:09:29
您可以使用棱镜事件聚合器来确保您在UI线程上。见此处:http://neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html
https://stackoverflow.com/questions/8578346
复制相似问题