I'm trying to navigate from my ViewModel to a page.
I followed this tut: https://marcominerva.wordpress.com/2015/10/07/navigationservice-with-mvvm-light-and-template-10/
But doing : NavigationService.Navigate(Pages.MainPage); from my ViewModel doesn't work, NavigationService is null (my ViewModel inherits ViewModelBase)
Any thought?
Have you tried the sample from the repo?
Yes but the sample doesn't call NavigationService.Navigate from the viewmodel.
Anyway, I solved my problem with a simple workaround: I injected the NavigationService instance from the Bootstrap class in my DI so that I can use it in my ViewModel.
In the App class:
public override Task OnInitializeAsync(IActivatedEventArgs args)
{
DispatcherHelper.Initialize();
SimpleIoc.Default.Register(() => NavigationService);
return base.OnInitializeAsync(args);
}
In my ViewModel:
public StartTripViewModel(INavigationService navigationService)
{
NavigationService = navigationService;
StartTripCommand = new RelayCommand(async () =>
{
var contacts = await GetContacts();
NavigationService.Navigate(Types.Pages.Contact, contacts);
});
}
Hi bbougot!
I actually use NavigationService from ViewModel without problems. If you send me an email from https://marcominerva.wordpress.com/contacts/, I would be happy to help you identifying the problem.
@bbougot Your solution is possibly problematic because the bootstrapper will inject the navservice for you, and the version of the navservice it injects will be the correct thread and frame-ref. Your approach is okay for you, but is not generally a good practice.
The real question is why the navservice would EVER be null. It's important that you repro this for us so we can correct this for other cases like yours.
I'm currenlty in touch with bbgout to figure out the problem. As soon as we found the issue, I'll update this post with the details.
I have the same issue. Was there ever a resolution to this? My ViewModelBase implements INavigable, and my view models all inherit from ViewModelBase. However, when I call this.NaviationService.Navigate(typeof(MyPage), null); I see that this.NavigationService is null.
My app.cs inherits from Bootstrapper, and this doesn't have a problem...
I'm pretty sure you're calling Navigate(...) from a UserControl or a Page which you haven't navigated to. You have to call from a Page which has been navigated to.
I am though, I'm calling it from a button command on a page that's already been navigated to
OK - My viewmodel's OnNavigatedTo is not being hit. This is why it's null. However, none of my UserControls that inherit from ViewModelBase, hit the OnNavigatedTo function...
I can get a reference to the NavigationService adding the instance in the Shell to my container:
container.RegisterInstance
I can then use this in my ViewModel. However, I can't figure out why the OnNavigatedto method is not being hit, even though my ViewModelBase is implementing INavigable
same issue:
I have a page which contains a Pivot (FooPage).
The pivot has several pivotitems. Each pivot item has a usercontrol as content.
This because I didn't want to have all code in one big file, so I separated it to usercontrols).
It is similar to the SettingsParts from the SettingsPage(ViewModel).
The FooPageViewModel does not contain properties for all the Parts (e.g. there are no FooBar1PartViewModel, FooBar2PartViewModel properties like in SettingsPageViewModel.
Instead I just do:
<PivotItem>
<PivotItem.Header>
<local:TabHeader Label="Part 1" Glyph="" />
</PivotItem.Header>
<local:FooBar2PartUC />
</PivotItem>
FooBar2PartUC.xaml is a usercontrol with DataContext bound to its ViewModel property FooBar1PartViewModel in the ViewModelLocator.
Now is also my question:
When somebody clicks on an item from one of these Part viewmodels I need to navigate to a page showing the details. How do I get and use the INavigationService in my parts? Because 'this.NavigationService' is null.
(hope it is somewhat clear)
Update:
The only way I can access the NavigationService in the usercontrol is via the bootstrapper, but I'm not sure if this is the best thing to do:
App.Current.NavigationService.Navigate<App.Pages>(App.Pages.FooBar1Part, someParameter);
Hi guys!
I have had the same issue.
NavigationService was null when I defined and initialized my ViewModel as property in code behind.
Solution is to define it in xaml (<Page.DataContext>) then everything works like a charm.
@kyuri93 why do you need it code behind of your View
@mvermef I don't, but i had to reuse view from different project, not based on Template 10. Where ViewModel where defined in code behind.
So I had to spend a bunch of time investigating why NavigationService is null.
Trying to save that time for others.
Most helpful comment
same issue:
I have a page which contains a Pivot (FooPage).
The pivot has several pivotitems. Each pivot item has a usercontrol as content.
This because I didn't want to have all code in one big file, so I separated it to usercontrols).
It is similar to the SettingsParts from the SettingsPage(ViewModel).
The FooPageViewModel does not contain properties for all the Parts (e.g. there are no FooBar1PartViewModel, FooBar2PartViewModel properties like in SettingsPageViewModel.
Instead I just do:
FooBar2PartUC.xaml is a usercontrol with DataContext bound to its ViewModel property FooBar1PartViewModel in the ViewModelLocator.
Now is also my question:
When somebody clicks on an item from one of these Part viewmodels I need to navigate to a page showing the details. How do I get and use the INavigationService in my parts? Because 'this.NavigationService' is null.
(hope it is somewhat clear)
Update:
The only way I can access the NavigationService in the usercontrol is via the bootstrapper, but I'm not sure if this is the best thing to do: