Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't navigate from inside a callback method with Prism
    primarykey
    data
    text
    <p>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.</p> <p>Now, some code: The viewmodel for my first module <code>LoginModule</code>:</p> <pre><code>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(() =&gt; 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&lt;UserAuthenticatedEvent&gt;().Publish(""); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } ... } </code></pre> <p>The ViewModel for my second module <code>RosterModule</code>:</p> <pre><code>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&lt;UserAuthenticatedEvent&gt;().Subscribe(o =&gt; { 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"); } } </code></pre> <p>Any tips on what I might be doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload