为 WebBrowser 控件添加后退和前进按钮

发布于 2024-12-11 10:21:24 字数 378 浏览 0 评论 0原文

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

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

Android 具有完全相同的方法名称来实现相同的目的。

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

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

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

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 ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单调的奢华 2024-12-18 10:21:24

这就是我最终的做法。

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

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

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;
}
朦胧时间 2024-12-18 10:21:24

我在包含网络浏览器的应用程序之一的页面的应用程序栏中添加了一个后退按钮。我希望应用栏中的后退按钮能够向后导航网页,并希望硬件后退按钮能够转到上一个 xaml 页面。这样,用户无需使用硬件后退按钮向后导航浏览器中所有访问过的网页即可返回到先前的 xaml 页面。这是我的做法,您可以轻松地设置前向堆栈,当用户单击后退(应用程序栏)按钮时,页面会从该堆栈弹出并被推送到前向堆栈。

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

我检查 NavigationStack.Count > 的原因是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.

探春 2024-12-18 10:21:24

对于 javascript 解决方案,它正在执行类似以下操作:

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

将 WebBrowser 元素的 IsScriptingEnabled 设置为 true。
然而,这总是会生成错误 80020006 的异常。我读了各种关于 DOCTYPE 如何成为罪魁祸首、系统缓存或在内容加载后设置 IsScriptEnabled 的帖子...它从来没有工作过...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文