0
votes

I'm trying to Invoke NavigateTo in Windows 8.1 Universal app outside of the wiew model. I already tried:

var dispatcher = Window.Current.Dispatcher;

await dispatcher.RunAsync(
    CoreDispatcherPriority.Normal, 
    async () => await NavigateToRegistrationPage()
);

Resulting with exception thrown

Object reference not set to instance of object.

I also tried Using Messenger to invoke navigation form ViewModel via messenger message:

Messenger.Default.Send<MessengerEvents.NavigateToRegistrationPage>(
    new MessengerEvents.NavigateToRegistrationPage()
);

Public LoginViewModel //...

MessengerInstance.Register<MessengerEvents.NavigateToRegistrationPage>(
    this,
    (message) => GoToRegistrationPage()
); 

Doesn't work either.

Error has been thrown by the target of invocation

I cant invoke navigation directly in viewmodel (checked without messenger called directly from the view model works perfectly) as navigation is performed in response to server message.

private void GoToRegistrationPage() 
{
    NavigationService.NavigateTo(ResourceService.CHOOSE_REGISTRATION_PAGE); 
}
1
Depending on how you are using MVVM-Light, there should be an IOC container from which you can request the current instance of the navigationservice. And use that to do the same actual navigation as inside your viewmodelsDepechie
ServiceLocator.Current.GetInstance<INavigationService>(); would be the code most likelyDepechie
@Depechie When i tried this i checked that i get the object but "currentPageKey" is null" I have this page in CreateNavigationService navigationService.Configure(ResourceService.CHOOSE_LOGIN_PAGE, typeof(LoginPage)); still no idea how to solve it instance.currentPageKey threw exception of type System.NullReferenceExceptionGroth
Hmm a bit unclear what is going on just by reading this commentDepechie

1 Answers

0
votes

Problem solved, I tried to grab dispatcher from MVVM Locator, moved dispatcher to app.xaml.cs and works perfectly