1
votes

I've implemented the following pattern with MVVM Light: Calling ViewModel methods in response to Page navigation events using MVVM Light in WinRT

I use this in combination with:How to handle the back button on WP 8.1 using MVVM light?

private static INavigationService CreateNavigationService()
{
    var navigationService = new NavigationService();
    navigationService.Configure("Details", typeof(DetailsPage));
    navigationService.Configure("Chart", typeof(ChartPage));

    // Handle back button
    HardwareButtons.BackPressed += (sender, args) => {
        navigationService.GoBack();
        args.Handled = true;
    };

    return navigationService;
}

Navagtion to the Details page and/or Chart page works, also the back button works. But after that, I'm not able to navigate to one of the pages again.

It looks like the MainPage and its ViewModel aren't reloaded (cache?). So they lose their bindings. The RelayCommand isn't set, so navigation isn't possible anymore.

Can anyone help me with this?

Edit: found the solution :)

Best regards, Rick

1

1 Answers

2
votes

Changed the code in the ViewModel from:

public MainViewModel(INavigationService navigationService)
{
    _navigationService = navigationService;

    DetailsCommand = new RelayCommand(() =>
    {
        navigationService.NavigateTo("Details", "My data");
    });
}

To:

public MainViewModel(INavigationService navigationService)
{
    _navigationService = navigationService;

    DetailsCommand = new RelayCommand(() =>
    {
        _navigationService.NavigateTo("Details", "My data");
    });
}

And navigation works now back and forth.