0

All day long, I'm calling

await this.NavigationService.NavigateAsync(typeof(Views.MyStankView));

And everything works great. Until I x:Bind a button click to another viewmodel that inherits from (MVVM-Light's) ViewModelBase and (Template10's) INavigable that has a method

public async Task NavigateToMyStankView(object sender, RoutedEventArgs e)
{
    await this.NavigationService.NavigateAsync(typeof(Views.MyStankView));
}

The call to

await this.NavigationService.NavigateAsync(typeof(Views.MyStankView));

gets executed, but I don't GO anywhere. From within this viewmodel and it's parent, neither OnNavigatingFrom get's called. No errors, no navigation.

What gives? Why is a bound/injected viewmodel's NavigateAsync not navigating? While a "hard" viewmodel does navigate?

EDIT: Upon further inspection, this.NavigationService is null?!? Why so?

Maelstrom
  • 71
  • 9

1 Answers1

0

This is expected behavior. The NavigationService property of your view-model is injected by the NavigationService during navigation. Something like ViewModel.NavigationService = this;. If you are injecting a view-model, then you are creating it, not the NavigationService, and so the property is null. But that's not the end of the world. You can easily work-around this by setting the NavigationService property of your view-model yourself, using the property value from the main view-model as your source. Something like this var vm = new MyViewModel { NavigationService = this.NavigationService, Dispatcher = this.Dispatcher };.

A good rule of thumb is this, if the NavigationService property is null, you won't be able to navigate with it. ;-) Just teasing. I hope this work-around makes sense.

Best of luck.

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • Thanks for the reply, Jerry. You mean like this? await this.NavigationService.NavigateAsync(typeof(Views.MyStankView), new MyStankViewModel { NavigationService = this.NavigationService }); that results in this (very apropos) error: Message: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.ni.dll – Maelstrom Nov 02 '16 at 13:16