在异步WCF响应线程上使用PRISM 4请求UI导航

我正在开发一个使用以下技术的项目:

  • C#(.NET 4.0)
  • WCF
  • 棱镜4

我目前正在使用代理生成的Begin / End方法对我们的一个Web服务进行异步调用。 调用成功,客户端能够在工作线程上接收Web服务的响应。

收到回复后,我继续举办活动。 订阅该事件的类继续使用PRISM请求UI导航:

Application.Current.Dispatcher.BeginInvoke(new Action(() => this.RegionManager.RequestNavigate(RegionNames.LoginContentRegion, projectSelectionViewUri))); 

由于未在UI线程上捕获异步WCF响应,因此我不得不使用Application.Current.Dispatcher.BeginInvoke(…)调用UI线程。

这里的问题是调用似乎什么都不做。 UI未更新,并且不会引发exception。

使用调度程序尝试调用的类是View的View-Model。 它是使用控制反转(使用UNITY容器)创建的。

这是视图的构造函数,它请求它的View-Model:

  public CredentialsInputView(ICredentialsInputViewModel viewModel) { InitializeComponent(); ViewModel = viewModel; ... } 

上面的代码会调用View-Model的构造函数。 我尝试将调度程序存储在VM的构造函数调用中,但是稍后使用它来调用UI导航似乎没有帮助。 我认为在UI线程上没有创建View-Model:

  private static System.Windows.Threading.Dispatcher dispatcher; ///  /// Initializes a new instance of the  class. public CredentialsInputViewModel(ICodexLoginService codexLoginService, ISessionService sessionService, IRegionManager regionManager) { dispatcher = Application.Current.Dispatcher; ... } 

我应该如何从工作线程中引发的事件中调用UI线程?

您可以使用prism事件聚合器来确保您位于UI线程上。 见这里: http : //neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html