11

I have a small application using WPF and Prism. I have my shell and two modules. I can successfully navigate between them in the "normal fashion" (e.g from a button click) so I know they are wired up for navigation correctly. However, if I perform some asynchronous operation that fires an event on completion, I can't navigate from inside that event handler. The last thing I tried was using Event Aggregation to publish an event back to the UI thread, but it's still not navigating. The Subscriber to the event gets the event successfully and fires RequestNavigate(...) but the UI doesn't update.

Now, some code: The viewmodel for my first module LoginModule:

public class LoginViewModel : ViewModelBase, ILoginViewModel, INavigationAware
{
    ...

    [ImportingConstructor]
    public LoginViewModel(IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator)
    {
        _regionManager = regionManager;
        _container = container;
        _eventAggregator = eventAggregator;
    }

    private DelegateCommand _Login;
    public DelegateCommand Login
    {
        get
        {
            if (_Login == null)
                _Login = new DelegateCommand(() => LoginHandler());
            return _Login;
        }
    }
    private void LoginHandler()
    {
        _client = new JabberClient();
        _client.Server = "gmail.com";
        _client.User = Username;
        _client.Password = Password;

        ...

        _client.OnAuthenticate += client_OnAuthenticate;
        _client.Connect();
    }

    private void client_OnAuthenticate(object sender)
    {
        Console.WriteLine("Authenticated!");
        _eventAggregator.GetEvent<UserAuthenticatedEvent>().Publish("");
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }
    ...
}

The ViewModel for my second module RosterModule:

public class RosterViewModel : IRosterViewModel, INavigationAware
{
    private readonly IEventAggregator _eventAggregator;
    private readonly IRegionManager _regionManager;

    [ImportingConstructor]
    public RosterViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
    {
        _regionManager = regionManager;
        _eventAggregator = eventAggregator;

        _eventAggregator.GetEvent<UserAuthenticatedEvent>().Subscribe(o =>
        {
            Console.WriteLine("Requesting navigation...");
            _regionManager.RequestNavigate(RegionNames.ContentRegion, new Uri(WellKnownViewNames.RosterView, UriKind.Relative));
        });
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {

    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        Console.WriteLine("I'm here at the RosterViewModel");
    }
}

Any tips on what I might be doing wrong?

3
  • why didn't you put it as an answer? Commented Oct 18, 2013 at 6:46
  • 5
    Please copy your resolved comments into an answer, and answer it. This could help someone else some day. Commented Oct 23, 2013 at 17:08
  • 1
    @Jason: Please add your solution to the answer and mark it as answer.If you don't mark it as an answer, the people who are looking for the solution of same problem will not look into this post as it is shown as unanswered in the list. Also the people who knows the solution and willing to answer it will get disappointed after investing their time to read this post. Commented Dec 5, 2013 at 6:40

1 Answer 1

2

From OP,

Ok, so just a few minutes after posting, I reread an article I ran into yesterday and saw something I missed...

http://neverindoubtnet.blogspot.com/2009/05/event-aggregator-in-prism-explorer.html

They explain that one of the overloads of the Subscribe method includes a ThreadOption.

So:

_eventAggregator.GetEvent<UserAuthenticatedEvent>()
    .Subscribe(
    o =>
    {
        Console.WriteLine("Requesting navigation...");
        _regionManager.RequestNavigate(
            RegionNames.ContentRegion,
            new Uri(WellKnownViewNames.RosterView, UriKind.Relative));
    });

Became:

_eventAggregator.GetEvent<UserAuthenticatedEvent>()
    .Subscribe(
    o =>
    {
        Console.WriteLine("Requesting navigation...");
        _regionManager.RequestNavigate(
            RegionNames.ContentRegion,
            new Uri(WellKnownViewNames.RosterView, UriKind.Relative));
    }, 
    ThreadOption.UIThread);

And now it works!

Hopefully, this helps someone else down the road.

Enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.