I am using a web-browser in my windows phone 7 application. I just want to know that how to handle its back and forward navigation just like any desktop browser. And also how to block a particular navigation.
I referred here and many others but couldn't find anything working for me. Please help.
You can cancel navigation by handling the OnNavigating event
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel navigation
e.Cancel = true;
}
To go back, you can execute javascript on the page.
webBrowser.InvokeScript("eval","history.go(-1)");
and move forward:
webBrowser.InvokeScript("eval","history.go(1)");
If eval
is blocked on the page, this site might be useful for alternatives. Don't forget to set webBrowser.IsScriptEnabled to true
.
To make it work like a desktop browser you can implement a stack
.
You will put two buttons back and forward. As the user navigate to next URL in browser you push them into history stack and when he wants to get back by pressing back button you navigate to the first url in the history stack programmatically and pop it up from the history stack so he can navigate back till stack has some URLs in it. Similarly for forward you push URLs into forward stack as he navigates back and and whenever he presses forward button you navigate to first element in forward stack and pop it up and so on till you have urls left in forward stack. Once he navigates to some url which is not navigated from forward stack by you then you empty the forward stack and fill it again when he moves back.
This way you can even show history urls in a list or however you like.
About navigation cancel, here is the code from the link to question in the comments under your question, it should work.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel navigation
e.Cancel = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With