为 WebBrowser 控件添加后退和前进按钮 [英] Adding back and forward button for WebBrowser control

查看:41
本文介绍了为 WebBrowser 控件添加后退和前进按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中有一个 WebBrowser 元素,我想向其中添加一个后退和前进按钮,并在没有可返回和可前进的情况时禁用这些按钮.

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.

在 Cocoa 中,UIWebView 具有可以轻松检查的方法:canGoBack 和 canGoForward,并且您可以使用 goBack 和 goForward 方法(以及重新加载等.)

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 具有完全相同的方法名称来实现相同的目的.

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

我看到这些方法在 .Net 4 和 3.5 SP1 中可用.

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

我找到了一些关于在 Silverlight 中使用 javascript 命令的参考资料,但我发现这非常麻烦,而且无法检测历史记录中是否有任何内容(当然除非我自己管理)

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)

当然,Windows Phone 中有一些更高级的东西..

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

推荐答案

这是我最终的做法.

这里假设您已经设置了 backButtonforwardButton;这些按钮的状态将根据您在导航堆栈中的位置进行相应更新.

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.

webViewWebBrowser 对象

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;
}

这篇关于为 WebBrowser 控件添加后退和前进按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆