2
我如何從瀏覽器控件訪問歷史記錄和Cookie,目前使用列表框來添加會話中的每個網站,但它不是非常可靠,並且不會使用後退按鈕。來自瀏覽器控件的訪問歷史記錄和其他內容
在windows phone上還有一個後退按鈕的代碼嗎? GoBack();
不起作用。
我如何從瀏覽器控件訪問歷史記錄和Cookie,目前使用列表框來添加會話中的每個網站,但它不是非常可靠,並且不會使用後退按鈕。來自瀏覽器控件的訪問歷史記錄和其他內容
在windows phone上還有一個後退按鈕的代碼嗎? GoBack();
不起作用。
您可以將您在瀏覽器控件中導航的頁面添加到應用程序的歷史堆棧,以便用戶可以使用手機的後退按鈕導航回去。
我在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並在Web瀏覽器控件的堆棧中導航。
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;
}
}
}
請注意,仍然有一些邊緣案例不能很好地實現。正如你所看到的,代碼是微不足道的,但是它有一個問題不能解決。它無法區分:
總之,這裏是一個總結:
我希望你能用這個做點什麼。
(全部學分海梅·羅德里格斯誰寫的博客和代碼我只是張貼了什麼,他寫了一個摘要。)
在「開放的目的地= history.Peek(); web.Navigate(目的地) ; e.Cancel = true;「 -----紅色突出顯示「Peek();」並說它不在字典 – Ravi
好吧,我會盡快看到與Visual Studio的計算機。 – Styxxy
Peek是Stack的一個有效方法 class:http://msdn.microsoft.com/en-us/library/1w32446f.aspx。 –
Styxxy