如何拦截在用户点击一个链接在一个网页浏览器 [英] How to intercept when user click on a link in a webbrowser
问题描述
我试图拦截窃听在WebBrowser控件中的链接。
我的HTML页面中包含自定义链接,对于一些开始共享://我想拦截时
I'm trying to intercept tapping on a link in a WebBrowser control. My HTML page contains custom links, for some starting with shared:// I'd like to intercept when the user tap on it.
这是它的用户水龙头在iPhone上我。会使用web视图:shouldStartLoadWithRequest:navigationType:方法,并期待在所选择的网址
On iPhone I would use the webView:shouldStartLoadWithRequest:navigationType: method, and look at the URL that is selected.
我还没有成功地再现了类似的行为与Silverlight for Windows Phone支持
I haven't managed to reproduce a similar behaviour with Silverlight for Windows Phone.
我做这样的事情:
{
webBrowser1.Navigating += new EventHandler<NavigatingEventArgs>(webBrowser1_Navigating);
}
void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
string scheme = null;
try
{
scheme = e.Uri.Scheme; // <- this is throwing an exception here
}
catch
{
}
if (scheme == null || scheme == "file")
return;
// Not going to follow any other link
e.Cancel = true;
if (scheme == "shared")
{
}
但我想读URI的某些属性,当一个例外,当它是一个开放的标准使用默认的文件:// URL
此外,甚至没有触发开始链接导航事件共享://
But I guess an exception when reading some properties of the Uri, when it's a standard Uri with a default file:// URL Additionally, the Navigating event isn't even triggered for links starting with shared://
现在我能够捕捉到在窃听共享://我并不太在乎,但至少我希望能检索我们要找到并取消默认操作为特定的URL网址。
Now that I'm able to capture tapping on a shared:// I do not care much, but at least I'd like to be able to retrieve the URL we're going to navigate to, and cancel the default operation for a particular URL.
任何想法是怎么回事?
谢谢
Any ideas what's going on? Thanks
编辑:
原来,问题是,只对以下链接生成导航事件:文件:// http://或至mailto://
URI的方案属性仅可用于HTTP://和至mailto://链接
It turned out that the problem is that the Navigating event is only generated for the following links: file://, http:// or mailto:// The scheme attributes of the Uri is only available for the http:// and mailto:// links
这样我到底做是更换共享:与访问http //链接://共享/嗒嗒 ......我看的URL。 ..这适用于我的目的。我现在可以有不同的动作(如打开一个额外的窗口)根据在HTML中的链接的链接。
so what I did in the end is replace the shared:// link with http://shared/blah ... And I look at the URL... This works for my purpose. I can now have links that have a different action (like opening an extra window) depending on the links in the html.
推荐答案
这是我最后的代码,如果这是有人在将来有用的:
Here is my final code, in case this is useful for someone in the future:
对于有关屏幕,我用了WebBrowser组件显示的HTML文件。
关于页面有一个告诉你关于这个程序的朋友链接以及链接到外部网站。
它还具有本地子页
For an about screen, I use an html file displayed in a WebBrowser component. The about page has a "tell your friend about this app" link as well as links to external web site. It also has local subpages.
本地子页面链接到使用文件://链接。这些可以在web浏览器组件中进行导航。
外部链接与Internet Explorer外部打开。
告诉你的友情链接是由为http://共享链接,与打开电子邮件预先设定的主题和正文。不幸的是,没有其他方案比标准的是可用的,因为他们不触发导航事件
Local sub-pages are linked to using a file:// link. Those can be navigated within the WebBrowser component. External links are opened externally with Internet Explorer. Tell your friend link is made of a http://shared link, that opens an email with a pre-set subject and body. Unfortunately, no other scheme than the standard ones are usable as they do not trigger a Navigating event
还有一个支持链接是一个mailto://链接,打开一个EmailComposeTask
There's also a support link which is a mailto:// link and opens an EmailComposeTask
void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
String scheme = null;
try
{
scheme = e.Uri.Scheme;
}
catch
{
}
if (scheme == null || scheme == "file")
return;
// Not going to follow any other link
e.Cancel = true;
if (scheme == "http")
{
// Check if it's the "shared" URL
if (e.Uri.Host == "shared")
{
// Start email
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "Sharing an app with you";
emailComposeTask.Body = "You may like this app...";
emailComposeTask.Show();
}
else
{
// start it in Internet Explorer
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri(e.Uri.AbsoluteUri);
webBrowserTask.Show();
}
}
if (scheme == "mailto")
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = e.Uri.AbsoluteUri;
emailComposeTask.Show();
}
}
这篇关于如何拦截在用户点击一个链接在一个网页浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!