使用PRISM 4异步WCF响应线程请求UI导航
我正在上使用了以下技术项目:
I'm working on a project which uses the following technologies:
- C#(.NET 4.0)
- WCF
- PRISM 4
我目前正在做的异步调用使用由代理生成的开始/结束的方法我们的网络服务之一。调用成功,客户能够接收Web服务的一个工作线程响应。
I'm currently making an asynchronous call to one of our Web Services using the Begin/End methods generated by a proxy. The call is successful and the client is able to receive the Web Service's response on a worker thread.
一旦收到响应,我继续引发事件。订阅事件的类进行请求UI导航使用PRISM:
Once the response is received, I proceed to raise an event. The class subscribed to the event proceeds to request a UI navigation using PRISM:
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
this.RegionManager.RequestNavigate(RegionNames.LoginContentRegion, projectSelectionViewUri)));
由于异步WCF响应没有在UI线程上拍摄的,我被迫要调用的UI线程使用Application.Current.Dispatcher.BeginInvoke(...)。
Since the asynchronous WCF response is not captured on the UI thread, I'm forced to invoke the UI thread using Application.Current.Dispatcher.BeginInvoke(...).
这里的问题是,调用似乎什么都不做。用户界面不更新,并不会抛出异常。
The problem here is that the invoke seems to do nothing. The UI is not updated, and no exception is thrown.
使用调度程序,它试图在调用类是视图的视图模型。它是利用控制反转(与UNITY容器)中创建
The class which attempts an Invoke using the dispatcher is a View's View-Model. It is created using inversion of control (with the UNITY container).
下面是视图的构造函数,它要求其视图模型:
Here is the view's constructor, which requests its View-Model:
public CredentialsInputView(ICredentialsInputViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
...
}
前面的代码将导致视图模型的构造被称为。我试图存储虚拟机的构造函数调用内的调度员,但使用它来调用UI导航后来似乎没有帮助。我认为,是不是在UI线程上创建的视图模型:
The preceding code causes the View-Model's constructor to be called. I tried storing the dispatcher within the VM's constructor call, but using it to invoke the UI navigation later on didn't seem to have helped. I take it that the View-Model is not created on the UI thread:
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线程从内部那是在一个工作线程引发的事件?
How should I invoke the UI thread from within an event that is raised on a worker thread?
您可以使用棱镜事件聚集,以确保您的UI线。在这里看到: http://neverindoubtnet.blogspot.com/ 2009/05 /事件汇聚功能于棱镜explorer.html
You can use the prism event aggregator to ensure that you are on the UI thread. See here: http://neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html