为 WebBrowser 控件添加后退和前进按钮
我在页面中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我最终的做法。
这假设您已经设置了
backButton
和forwardButton
;这些按钮的状态将根据您在导航堆栈中的位置进行相应更新。webView
是WebBrowser
对象Here is how I ended up doing it.
This assumes you have set a
backButton
andforwardButton
; the status of these buttons will be updated accordingly depending on where you are in the navigation stack.webView
is theWebBrowser
object我在包含网络浏览器的应用程序之一的页面的应用程序栏中添加了一个后退按钮。我希望应用栏中的后退按钮能够向后导航网页,并希望硬件后退按钮能够转到上一个 xaml 页面。这样,用户无需使用硬件后退按钮向后导航浏览器中所有访问过的网页即可返回到先前的 xaml 页面。这是我的做法,您可以轻松地设置前向堆栈,当用户单击后退(应用程序栏)按钮时,页面会从该堆栈弹出并被推送到前向堆栈。
我检查 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.
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.
对于 javascript 解决方案,它正在执行类似以下操作:
将 WebBrowser 元素的 IsScriptingEnabled 设置为 true。
然而,这总是会生成错误 80020006 的异常。我读了各种关于 DOCTYPE 如何成为罪魁祸首、系统缓存或在内容加载后设置 IsScriptEnabled 的帖子...它从来没有工作过...
In regards to the javascript solution it is doing something like this:
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...