0

I launch a page on UWP thanks to PageRenderer, this page allows take picture and I want go back when user take a pciture.

My problem is when I go back with hardware button I have no problem but with function goback doesn't work.

This is my code :

 var frame = Window.Current.Content as Frame;
                if (frame != null && frame.CanGoBack)
                {
                    frame.GoBack();
                }

CanGoBack return false.

So you have some idea

Thanks

Kevin Vincent
  • 587
  • 13
  • 28
  • Try to subscribe to *SystemNavigationManager.GetForCurrentView().BackRequested* event. – Romasz Jun 30 '16 at 11:03
  • This function is for hardware button and my problème is on a classic button. But I try and I can't beacause cangoback() return false : var frame = Window.Current.Content as Frame; Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += (s, a) => { Debug.WriteLine("BackRequested"); if (frame.CanGoBack) { frame.GoBack(); a.Handled = true; } }; I think my problem is on my frame instance – Kevin Vincent Jun 30 '16 at 11:42
  • What do you have in *Window.Current.Content*? Frame/Page? Have you debuged your code? When is your code fired? (there are some cases when the frame cannot go back) – Romasz Jun 30 '16 at 11:45
  • I use Xamarin and a pageRenderer, so Window.Current.Content allow me to get my current frame (if i say it's a page I have this exception : System.AccessViolationException) Yes debug my code and my problem is on goback() If I can't go back how I can close my frame ? I think it's possible because hardware button work. – Kevin Vincent Jun 30 '16 at 11:54
  • A suggestion, New CameraCaptureUI https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/capture-photos-and-video-with-cameracaptureui is available which works fine with going back and storing data with Xamarin. – Vishnu Jul 01 '16 at 10:42
  • I don't know about Xamarin but see [c# - Handling Back Navigation Windows 10 (UWP) - Stack Overflow](http://stackoverflow.com/questions/31832309/handling-back-navigation-windows-10-uwp). Perhaps this is actually a duplicate of that. – Sam Hobbs Apr 24 '17 at 00:26

2 Answers2

0

You need to hook into the back button pressed and then force the navigation back

        public PageContainer()
        {
            this.InitializeComponent();
            SystemNavigationManager.GetForCurrentView().BackRequested += PageContainer_BackRequested;
        }

        /// <summary>
        /// Handles when the user presses the back button 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageContainer_BackRequested(object sender, BackRequestedEventArgs e)
        {
             Frame rootFrame = Window.Current.Content as Frame;
             if (rootFrame == null){
                return;
             }

            // Navigate back if possible, and if the event has not 
            // already been handled .
            if (rootFrame.CanGoBack && e.Handled == false)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }

This example is an adaptation of the example provided on the MSDN website

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
-1

You have to use the BackPressed Event.

public Test()
{
    HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

private void Test_BackPressed(object sender, BackPressedEventArgs e)
{
      if (this.Layer.CanGoBack) 
      { 
          e.Handled = true; 
          this.Layer.GoBack(); 
      }
}
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111