如何从浏览器控件访问历史记录和Cookie,目前使用的是一个列表框,该列表框会添加会话中的每个站点,但它不是非常可靠,并且无法使用后退按钮。
Windows手机上是否还有后退按钮的代码? GoBack();
不起作用。
答案 0 :(得分:3)
您可以将在webbrowser控件中导航的页面添加到应用程序的历史堆栈中,以便用户可以使用手机的后退按钮进行导航。
我在MSDN博客上发现了一篇关于此事的非常有趣的文章,可以找到here。我将发布一小段代码作为评论。
1)收听WebBrowser.Navigated事件;跟踪已访问过的页面。
Stack<Uri> history= new Stack<Uri>();
Uri current = null;
private void WebBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
Uri previous = null;
if (history.Count > 0)
previous = history.Peek();
// This assumption is NOT always right.
// if the page had a forward reference that creates a loop (e.g. A->B->A ),
// we would not detect it, we assume it is an A -> B -> back ()
if (e.Uri == previous)
{
history.Pop();
}
else
{
if (current != null)
history.Push(current);
}
current = e.Uri;
}
2)在页面上收听OnBackKeyPress。如果WebBrowser具有导航堆栈,请取消backkeypress并在webbrowser控件的堆栈中导航。
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
if (!isPerformingCloseOperation)
{
if (history.Count > 0)
{
Uri destination = history.Peek();
webBrowser.Navigate(destination);
// What about using script and going history.back?
// you can do it, but
// I rather use that to keep ‘track’ consistently with our stack
e.Cancel = true;
}
}
}
请注意,仍有一些边缘情况未能很好地实现。
正如您所看到的,代码很简单,但它有一个未解决的问题。它无法区分:
总而言之,这是一个总结:
我希望你能用这个做点什么。
(撰写博客和代码的Jaime Rodriguez的全部学分。我只是发表了他所写内容的摘要。)