Handling the hardware button back and sending it to the WebBrowser control on a Windows Phone

I have a web browser control built into PhoneApplicationPage. I have to process the hardware return button and make the web browser return.

I know how to handle the equipment return button.

How to make the web browser go to the previous page? Apparently, the simple property of GoBack() and CanGoBack on the WinForms web browser is missing on Windows Phone.

+6
windows-phone-7 webbrowser-control
source share
2 answers

If you enable the script in the WebBrowser by setting IsScriptEnabled="true" , you can use the following to move backward in the browser:

 private void backButton_Click(object sender, EventArgs e) { try { browser.InvokeScript("eval", "history.go(-1)"); } catch { // Eat error } } 

You can find this code (and a bit more) in the blog post of Shaw Wildermuth Navigation using the WebBrowser control on WP7 .

+4
source share

I just looked at the same issue inside Overflow7

I decided to handle this in C # and not in Javascript. Basically, on my page, I added a Uri's stack:

  private Stack<Uri> NavigationStack = new Stack<Uri>(); 

then I intercepted the navigational event of the web browser:

  void TheWebBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { NavigationStack.Push(e.Uri); } 

and then on the back button, press the override button. I am trying to navigate using the back button if I can:

  protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { if (NavigationStack.Count > 1) { // get rid of the topmost item... NavigationStack.Pop(); // now navigate to the next topmost item // note that this is another Pop - as when the navigate occurs a Push() will happen TheWebBrowser.Navigate(NavigationStack.Pop()); e.Cancel = true; return; } base.OnBackKeyPress(e); } 

Please note that this solution does not work perfectly with supervision, not with ajax sites, but overall it works very well.

+9
source share

All Articles