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.
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;
}
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
.
Previous answer will work, but you can make a workable solution even simpler, since the WebBrowser control has an internal navigation stack.
Add forward and back button controls to your UI. In the action routine for each of them:
private void forwardButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
WebBrowser editWB = sender as WebBrowser;
if (editWB != null && editWB.CanGoForward)
editWB.GoForward();
}
You can get fancier: in the WebBrowser.LoadComplete
event (not the FrameworkElement.Loaded
event), you could test .CanGoForward and .CanGoBack and selectively enable or disable the buttons appropriately.
I tried to implement the idea proposed by BobHy but failed as in my WP7 / VS2010 Express I did not find the method CanGoForward for WebBrowser. I am using it inside of a canvas in user control which might impact the experience.
However, I found a straight forward detailed example at http://developer.nokia.com/community/wiki/WebBrowser_Control_Techniques_in_Windows_Phone implementing a stack as suggested by Parveen that works for me fine.