[英]Windows Phone 8 Back button to go back in WebBrowser
我制作了一个WebBrowser,它除了按后退按钮后的后退按钮以外,其他都可以正常运行,但应用程序将关闭并且不会返回历史记录。 我该如何解决这个问题? 我在互联网上找到了解决方案,但它们似乎没有用。
public MainPage()
{
this.InitializeComponent();
this.webBrowser.Navigate(new Uri("http://www.google.com", UriKind.Absolute));
this.webBrowser.LoadCompleted += webBrowser_LoadCompleted;
this.webBrowser.NavigationFailed += webBrowser_NavigationFailed;
this.webBrowser.IsScriptEnabled = true;
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
PS:那不是整个脚本,但我认为其余内容都是不必要的,如果不告诉我:) PPS:我是Windows Phone编程的新手。
Web浏览器只是页面内的控件,按设备后退按钮可导航回到上一页,如果只有一页,则退出应用程序。 因此,您将需要在返回键按下时停止页面导航。
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
}
这样可以防止向后导航
现在剩下的就是转到上一页,可以通过
webBrowser.InvokeScript("eval", "history.go(-1)" );
所以事件变成
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
试着做:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
WB1.InvokeScript("eval", "history.go(-1)");
e.Cancel = true;
}
当您覆盖OnBackKeyPress时,您不执行e.Cancel = true; 它将执行您的代码,但也会执行常规BackButton的操作-NavigateBack,Exit App等。 但是您必须记住要让用户具有退出应用程序或向后导航的能力,因此检查某些条件(例如,您的网络浏览器历史记录不为null)然后执行e.Canel更为合适,否则请退出该应用程序。
要在根目录中退出该应用程序(以便您可以批准认证要求),并且还可以返回导航,直到您在根目录中为止,请尝试此操作
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (MiniBrowser.CanGoBack){
e.Cancel = true;
MiniBrowser.InvokeScript("eval", "history.go(-1)");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.