我正在从事一个使用以下技术的项目:
- C#(.NET 4.0)
- WCF
- Prism 4
我目前正在使用代理生成的 Begin/End 方法对我们的 Web 服务之一进行异步调用。调用成功,客户端能够在工作线程上接收 Web 服务的响应。
收到响应后,我将继续引发事件。订阅事件的类继续使用 PRISM 请求 UI 导航:
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
this.RegionManager.RequestNavigate(RegionNames.LoginContentRegion, projectSelectionViewUri)));
由于异步 WCF 响应未在 UI 线程上捕获,我不得不使用 Application.Current.Dispatcher.BeginInvoke(...) 调用 UI 线程。
这里的问题是调用似乎什么都不做。 UI 没有更新,也没有抛出异常。
尝试使用调度程序进行调用的类是 View 的 View 模型。它是使用控制反转(使用 UNITY 容器)创建的。
这是 View 的构造函数,它请求它的 View 模型:
public CredentialsInputView(ICredentialsInputViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
...
}
前面的代码导致调用 View-Model 的构造函数。我尝试将调度程序存储在 VM 的构造函数调用中,但稍后使用它调用 UI 导航似乎没有帮助。我认为 View 模型不是在 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 线程?
最佳答案
您可以使用 prism 事件聚合器来确保您在 UI 线程上。看这里:http://neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html
关于c# - 在异步 WCF 响应线程上使用 PRISM 4 请求 UI 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8578346/