title: How to detect back button click and make webview go back
body: <p>I am trying to build a webview app for one of my projects using Xamarin but I can't seem to figure out how to make the webview element go to previous page instead of closing the app.</p>
  
  <p>So I have figured out how to detect the back button being pressed and prevent it from closing the app but I want to make the web page go back and if the web page can't go back then close the app.</p>
  
  <p>Here's my code at the moment:</p>
  
  <pre><code>using System;
  using Xamarin.Forms; 
  using Xamarin.Forms.Xaml; 
  using Application = Xamarin.Forms.Application; 
  
  namespace myNewApp
  {
      [XamlCompilation(XamlCompilationOptions.Compile)]
      public class WebPage : ContentPage
      {
          public object _browser { get; private set; }
  
          protected override bool OnBackButtonPressed()
          {
  
                  base.OnBackButtonPressed();
                  return true;
  
          }
  
          public WebPage()
          {
  
  
              var browser = new Xamarin.Forms.WebView();
  
              browser.Source = "https://myurl.com";
  
  
              Content = browser;
  
          }
  
  
  
      }
  }
  </code></pre>
  
  <p>I have tried a couple of answers and I found this bit of code but it doesn't work as the override can't access the public WebPage browser var:</p>
  
  <pre><code>if (browser.CanGoBack)
              {
                  browser.GoBack();
                  return true;
              }
              else
              {
                  base.OnBackButtonPressed();
                  return true;
              }
  </code></pre>
  
  <p>Any help would be much appreciated. </p>