接上篇《》,在做的时候就返现自己习惯性的使用后退键,评论的也说互动性不好,所以今天就弄了下让它支持后退键。
public partial class MainPage : PhoneApplicationPage { ////// 改为一个int计数器或许更好 /// private StackbroswerHistory; // Constructor public MainPage() { InitializeComponent(); webBrowser1.Navigated += new EventHandler (webBrowser1_Navigated); broswerHistory = new Stack (); } void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { broswerHistory.Push(webBrowser1.Source); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { webBrowser1.Navigate(new Uri("http://m.oschina.net/", UriKind.Absolute)); base.OnNavigatedTo(e); } protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { if (broswerHistory.Count > 1) { try { webBrowser1.InvokeScript("eval", "history.go(-1)"); broswerHistory.Pop(); } catch (Exception ex) { } finally { e.Cancel = true; } } else { MessageBoxResult result = MessageBox.Show(OSChinaResources.Tips_ExitApp, OSChinaResources.Tips_Title, MessageBoxButton.OKCancel); if (result == MessageBoxResult.Cancel) { e.Cancel = true; } } } }
嘿嘿。