windows-phone-8 – Windows Phone 8上的后退按钮

前端之家收集整理的这篇文章主要介绍了windows-phone-8 – Windows Phone 8上的后退按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法获得后面的硬件按钮来执行我希望它为 Windows Phone 8执行的操作.该应用程序严格来说只是webview,所以截至现在当单击后退(硬件)按钮时它会关闭应用程序.我如何解决这个问题,以便进入上一个网页或返回索引或其他内容

谢谢

这是我目前在MainPage.xaml.cs文件中的内容

  1. namespace AvoidDiabetes
  2. {
  3. public partial class MainPage : PhoneApplicationPage
  4. {
  5. // Url of Home page
  6. private string MainUri = "/Html/index.html";
  7. private Stack<Uri> _history = new Stack<Uri>();
  8. private Uri _current = null;
  9.  
  10. // Constructor
  11. public MainPage()
  12. {
  13. InitializeComponent();
  14. }
  15.  
  16. private void Browser_Loaded(object sender,RoutedEventArgs e)
  17. {
  18. // Add your URL here
  19. Browser.Navigate(new Uri(MainUri,UriKind.Relative));
  20. Browser.IsScriptEnabled = true;
  21. }
  22.  
  23. // Navigates back in the web browser's navigation stack,not the applications.
  24. private void BackApplicationBar_Click(object sender,EventArgs e)
  25. {
  26. Browser.GoBack();
  27. }
  28.  
  29. // Navigates forward in the web browser's navigation stack,not the applications.
  30. private void ForwardApplicationBar_Click(object sender,EventArgs e)
  31. {
  32. Browser.GoForward();
  33. }
  34.  
  35. // Navigates to the initial "home" page.
  36. private void HomeMenuItem_Click(object sender,EventArgs e)
  37. {
  38. Browser.Navigate(new Uri(MainUri,UriKind.Relative));
  39.  
  40.  
  41. }
  42.  
  43. // Handle navigation failures.
  44. private void Browser_NavigationFailed(object sender,System.Windows.Navigation.NavigationFailedEventArgs e)
  45. {
  46. MessageBox.Show("Navigation to this page Failed,check your internet connection");
  47. }
  48.  
  49.  
  50.  
  51. protected override void OnBackKeyPress(CancelEventArgs e)
  52. {
  53. base.OnBackKeyPress(e);
  54.  
  55.  
  56. if (_history.Count == 0)
  57. {
  58. // No history,allow the back button
  59. // Or do whatever you need to do,like navigate the application page
  60. return;
  61. }
  62. // Otherwise,if this isn't the first navigation,push the current
  63. else
  64. {
  65. Browser.GoBack();
  66. }
  67.  
  68.  
  69.  
  70.  
  71. }
  72.  
  73. private async void WebBrowser_Navigated(object sender,NavigationEventArgs e)
  74. {
  75. // If we navigated back,pop the last entry
  76. if (_history.Count > 0 && _history.Peek() == e.Uri)
  77. {
  78. _history.Pop();
  79. }
  80. // Otherwise,push the current
  81. else if (_current != null)
  82. {
  83. _history.Push(_current);
  84. }
  85.  
  86. // The current page is now the one we've navigated to
  87. _current = e.Uri;
  88. }
  89.  
  90. }

}

您需要在应用程序页面中覆盖 OnBackKeyPress并处理将 WebBrowser控件导航回上一页.

假设您正在使用C#,这里大概是如何做到的(将WebBrowser_Navigated连接到WebBrowser的Navigated事件):

  1. private Stack<Uri> _history = new Stack<Uri>();
  2. private Uri _current = null;
  3.  
  4. protected override void OnBackKeyPress(CancelEventArgs e)
  5. {
  6. base.OnBackKeyPress(e);
  7.  
  8. if (_history.Count == 0)
  9. {
  10. // No history,allow the back button
  11. // Or do whatever you need to do,like navigate the application page
  12. return;
  13. }
  14.  
  15. // Cancel the back button press
  16. e.Cancel = true;
  17.  
  18. // Navigate to the last page
  19. Browser.Navigate(_history.Peek());
  20. }
  21.  
  22. private async void WebBrowser_Navigated(object sender,NavigationEventArgs e)
  23. {
  24. // If we navigated back,pop the last entry
  25. if (_history.Count > 0 && _history.Peek() == e.Uri)
  26. {
  27. _history.Pop();
  28. }
  29. // Otherwise,push the current
  30. else if (_current != null)
  31. {
  32. _history.Push(_current);
  33. }
  34.  
  35. // The current page is now the one we've navigated to
  36. _current = e.Uri;
  37. }

猜你在找的Windows相关文章