简体   繁体   中英

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)
  • WCF
  • PRISM 4

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.

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)));

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.

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

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

How should I invoke the UI thread from within an event that is raised on a worker thread?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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