0
votes

I'm creating a WinPhone 8.1 app using Caliburn Micro 2 beta 2. The app works fine, but I have an issue with the back button in navigation: navigating to another view works, but if I press the hardware back button on the emulator to go back to the previous page once I have navigated, I'm bumped back to the OS start screen, as if the navigation stack were empty. In all the code samples I could find it's stated that implementing a GoBack method on my VM is enough for binding it to the navigation system via conventions, so it seems I'm not missing anything. If I put a breakpoint on the GoBack method but it is not hit at all, which suggests the issue should be rather connected with binding.

Here is a complete repro empty solution: http://1drv.ms/1gLAVil . This app is a sort of CM skeleton with 2 views, so that you can navigate from one to another and then press back.

My relevant code apart from the usual CM plumbing (found in App.xaml.cs) is just:

``` public bool CanGoBack { get { return _navigation.CanGoBack; } }

public void GoBack() { _navigation.GoBack(); } ```

Where _navigation is the standard implementation of INavigationService injected in the constructor of the base class for all my viewmodels. I tried with both the navigation methods I know of, but (as expected) nothing changes: the method NavigateToViewModel<OtherViewModel>() (which has an overload receiving one object parameter whose type should match the target VM's type of a property named Parameter, which is a quick way for passing complex objects between viewmodels), and UriFor<OtherViewModel>().Navigate() (which builds the URI-like string with optional, simple serializable parameters specified via .WithParam before Navigate()).

Could anyone suggest a solution?

1

1 Answers

0
votes

If you want to go back on the click of back button, you need to override the hardwarebutton event:

HardwareButtons.BackPressed += HardwareButtons_BackPressed;

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if(rootFrame != null && rootFrame.CanGoBack)
            {
                rootFrame.GoBack();
                e.Handled = true;
            }

        }

Make sure to set e.Handled to true.