简体   繁体   English

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

[英]Request UI navigation using PRISM 4 on an asynchronous WCF response thread

I'm working on a project which uses the following technologies: 我正在使用以下技术的项目中:

  • C# (.NET 4.0) C#(.NET 4.0)
  • WCF 世界足球联合会
  • PRISM 4 棱镜4

I'm currently making an asynchronous call to one of our Web Services using the Begin/End methods generated by a proxy. 我目前正在使用代理生成的Begin / End方法对我们的一个Web服务进行异步调用。 The call is successful and the client is able to receive the Web Service's response on a worker thread. 调用成功,并且客户端能够在辅助线程上接收Web Service的响应。

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: 预订事件的类继续使用PRISM请求UI导航:

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

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(...). 由于未在UI线程上捕获异步WCF响应,因此我不得不使用Application.Current.Dispatcher.BeginInvoke(...)来调用UI线程。

The problem here is that the invoke seems to do nothing. 这里的问题是,调用似乎无能为力。 The UI is not updated, and no exception is thrown. UI未更新,并且不会引发异常。

The class which attempts an Invoke using the dispatcher is a View's View-Model. 使用分派器尝试进行调用的类是View的View-Model。 It is created using inversion of control (with the UNITY container). 它是通过控制反转(使用UNITY容器)创建的。

Here is the view's constructor, which requests its View-Model: 这是视图的构造函数,它请求其视图模型:

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

The preceding code causes the View-Model's constructor to be called. 前面的代码将导致调用View-Model的构造函数。 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. 我尝试将调度程序存储在VM的构造函数调用中,但是稍后使用它来调用UI导航似乎没有帮助。 I take it that the View-Model is not created on the UI thread: 我认为没有在UI线程上创建View-Model:

    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;
        ...
    }

How should I invoke the UI thread from within an event that is raised on a worker thread? 如何在工作线程上引发的事件中调用UI线程?

You can use the prism event aggregator to ensure that you are on the UI thread. 您可以使用棱镜事件聚合器来确保您位于UI线程上。 See here: http://neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html 看到这里: http : //neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM