I'm searching for the appropiate way to handle the back button pressed event on Windows Phone 8.1 WinRT using the NavigationService
available on MVVM light 5.
So far I think the best place to do it is inside the ViewModelLocator
by registering the GoBack
method of the NavigationService
while creating it following the approach outlined in NavigationService in MVVM Light V5
This is an effective approach. However, I can't handle validation before navigating back so I was wondering if there is a more suitable way to handle this event.
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
// Register NavigationService
SimpleIoc.Default.Register(CreateNavigationService);
// Register ViewModels here
}
private INavigationService CreateNavigationService()
{
var navigationService = new NavigationService();
// Register pages here
navigationService.Configure("Details", typeof(DetailsPage));
// Handle back button
HardwareButtons.BackPressed += (sender, args) => {
navigationService.GoBack();
args.Handled = true;
};
return navigationService;
}
}