Navigation Issue

Mar 14, 2013 at 3:27 AM
Edited Mar 14, 2013 at 3:43 AM
I'm working an app that has the following flow:

List -> Item Detail -> Item Editor

One of the things you can do in the Item Editor is delete the item. If this happens the you should end up back at the List.

After the deletion happens I call INavigationService.GoBack from the Item Editor ViewModel. In the Item Detail ViewModel OnNavigatedTo handler I check if the navigationMode is Back and if the item no longer exists. If so, I again call INavigationService.GoBack. This should put me back on the List.

Unfortunately the call doesn't seem to work inside the OnNavigatedTo method. I've traced it in the debugger and the call is happening but the app don't navigate. Once the Item Detail page is displayed INavigationService.GoBack will take you back to the List.

Any ideas?

-Michael O. Schoneman
Apr 4, 2013 at 9:35 PM
Hi Michael,

I'm not aware of the detail about how frame navigation works in windows store apps, but based on your description, it seems that it might not be possible to invoke a navigation request when a previous request is being resolved. Hence, it might not be possible to invoke GoBack / GoForward inside the OnNavigatedTo and OnNavigatedFrom methods.

A simple approach could be to navigate specifically to the List view from the Item Editor, but if that is not possible, another approach could be to have a simple controller that could be in charge of invoking the GoBack method twice. Basically, the Item Editor could invoke a method or publish an event with the corresponding data to tell the controller that you need to navigate two views back in the navigation stack. The controller would then simply invoke the GoBack method twice synchronously, avoiding the problem of invoking it in the OnNavigatedTo method.

I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Apr 5, 2013 at 2:53 PM
Edited Apr 5, 2013 at 5:25 PM
It sure looks like it isn't possible to invoke a navigation request while the previous one is in process.

I wanted to keep the concept of whether the DetailViewModel is valid inside the DetailViewModel. I didn't think it was right for the EditorViewModel to know that it should navigate two back (since in the future you might be able to directly jump to the EditorViewModel).

What I ended up doing was creating a new FrameNavigationWithValidityCheckingService and a new INavigationValidityAware interface (still don't like the names, but haven't come up with something better). My GoBack method now looks like this:
            _frame.GoBack();

            var newView = _frame.Content as FrameworkElement;
            if (newView == null) return;
            var newViewModel = newView.DataContext as INavigationValidityAware;
            if (newViewModel != null)
            {
                if (!newViewModel.IsValid)
                    GoBack();
            }
-Michael O. Schoneman
Apr 5, 2013 at 3:10 PM
As a follow up I did have to make some changes to the MvvmAppBase class to make the CreateNavigationService method virtual. I'd love to see that change incorporated into an official drop.

-Michael O. Schoneman