Adding back and forward button for WebBrowser control

*爱你&永不变心* 提交于 2019-12-24 11:12:31

问题


I have a WebBrowser element in a page, to which I would like to add a back and forward buttons, and have those buttons disabled when there's nothing to go back to and nothing to go forward to.

In Cocoa, the UIWebView has methods to easily check that: canGoBack and canGoForward, and you have goBack and goForward methods available (along with reload etc..)

Android has the exact same method names for achieving the same.

I see those methods are available in .Net 4 and 3.5 SP1.

I've found some references about using javascript commands in Silverlight but I find this very cumbersome, plus there's no way to detect if there's anything in the history (unless of course I manage this myself)

Surely, there's something a tad more advanced in Windows Phone ..


回答1:


Here is how I ended up doing it.

This assumes you have set a backButton and forwardButton; the status of these buttons will be updated accordingly depending on where you are in the navigation stack.

webView is the WebBrowser object

List<Uri> HistoryStack;
int HistoryStack_Index;
bool fromHistory;

// Constructor
public HelpView()
{
    InitializeComponent();
    HistoryStack = new List<Uri>();
    HistoryStack_Index = 0;
    fromHistory = false;
    webView.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(WebView_Navigated);
    UpdateNavButtons();
}

private void backButton_Click(object sender, RoutedEventArgs e)
{
    if (HistoryStack_Index > 1)
    {
        HistoryStack_Index--;
        fromHistory = true;
        webView.Navigate(HistoryStack[HistoryStack_Index-1]);
        updateNavButtons();
    }
}

private void forwardButton_Click(object sender, RoutedEventArgs e)
{
    if (HistoryStack_Index < HistoryStack.Count)
    {
        HistoryStack_Index++;
        fromHistory = true;
        webView.Navigate(HistoryStack[HistoryStack_Index-1]);
        UpdateNavButtons();
    }
}

private void UpdateNavButtons()
{
    this.backButton.IsEnabled = HistoryStack_Index > 1;
    this.forwardButton.IsEnabled = HistoryStack_Index < HistoryStack.Count;
}

private void WebView_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    if (!fromHistory)
    {
        if (HistoryStack_Index < HistoryStack.Count)
        {
            HistoryStack.RemoveRange(HistoryStack_Index, HistoryStack.Count - HistoryStack_Index);
        }

        HistoryStack.Add(e.Uri);
        HistoryStack_Index++;
        UpdateNavButtons();
    }

    fromHistory = false;
}



回答2:


I have a back button added to the applicationbar of a page in one of my apps which contains a webbrowser. I wanted the back button in the app bar to take the web page navigation backward, and wanted the hardware back button to go to the previous xaml page. This way, the user doesn't have to use the hardware back button to navigate backward through all the visited web pages in the webbrowser in order to go back to the prior xaml page. Here is how I did it, and you could easily set up a forward stack and when the user clicks the back (appbar) button, the page pops from that stack and is pushed to the forward stack.

        private void NavigateWeb()
        {
              if (!loaded)
              {
                    NavigationStack.Clear();
                    try
                    {
                          Web.Source = new Uri("http://m.weightwatchers.com/");
                          loaded = true;
                    }
                    catch (Exception ex)
                    {
                          MessageBox.Show("Unable to navigate to page.\n" + ex.Message,
                                "Error", MessageBoxButton.OK);
                    }
              }
        }

        void Web_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
              NavigationStack.Push(e.Uri);
        }


        void btnBack_Click(object sender, EventArgs e)
        {
              if (NavigationStack.Count > 2)
              {
                    // get rid of the topmost item...
                    NavigationStack.Pop();
                    // now navigate to the next topmost item
                    // note that this is another Pop - as when the navigate occurs a Push() will happen
                    Web.Navigate(NavigationStack.Pop());
              }
        }

The reason I check for NavigationStack.Count > 2 is that the particular webpage that I'm showing in the webbrowser always starts with a "click here to continue" link on the first page, and there is no reason to go back to there. That's the downfall of showing other people's sites in your webbrowser - you don't have control over what is shown.




回答3:


In regards to the javascript solution it is doing something like this:

    private void backButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            webView.InvokeScript("eval", "history.go(-1);");
        }
        catch
        {
            // Eat error
        }

    }

    private void forwardButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            webView.InvokeScript("eval", "history.go(1);");
        }
        catch
        {
            // Eat error
        }
    }

with having the IsScriptingEnabled set to true for the WebBrowser element. However, this always generates an exception with error 80020006. I read various posts about how the DOCTYPE could have been the culprit, the system caching or IsScriptEnabled being set after the content was loaded... It just never worked...



来源:https://stackoverflow.com/questions/7873632/adding-back-and-forward-button-for-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!