gpt4 book ai didi

c# - 如何在用户单击 Web 浏览器中的链接时进行拦截

转载 作者:太空狗 更新时间:2023-10-29 20:28:36 32 4
gpt4 key购买 nike

我试图拦截对 WebBrowser 控件中链接的点击。我的 HTML 页面包含自定义链接,对于一些以 shared://开头的链接,我想在用户点击它时进行拦截。

在 iPhone 上,我会使用 webView:shouldStartLoadWithRequest:navigationType: 方法,并查看所选的 URL。

我还没有设法用 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 的某些属性时出现异常,当它是具有默认 file://URL 的标准 Uri 时此外,对于以 shared://

开头的链接,甚至不会触发 Navigating 事件

现在我可以捕获对共享的点击://我不太在意,但至少我希望能够检索我们要导航到的 URL,并取消默认设置针对特定 URL 的操作。

有什么想法吗?谢谢

编辑:事实证明,问题是 Navigating 事件仅针对以下链接生成:file://、http://或 mailto://Uri 的方案属性仅适用于 http://和 mailto://链接

所以我最后所做的就是将 shared://链接替换为 http://shared/blah ...然后我查看 URL...这对我有用。根据 html 中的链接,我现在可以拥有具有不同操作(如打开额外窗口)的链接。

最佳答案

这是我的最终代码,以防将来对某人有用:

对于“关于”屏幕,我使用了一个显示在 WebBrowser 组件中的 html 文件。关于页面有一个“告诉你的 friend 关于这个应用程序”的链接以及到外部网站的链接。它还具有本地子页面。

本地子页面使用 file://链接链接。这些可以在 WebBrowser 组件中导航。外部链接使用 Internet Explorer 从外部打开。告诉你的 friend 链接由 http://shared 组成链接,打开带有预设主题和正文的电子邮件。不幸的是,除了标准方案外没有其他方案可用,因为它们不会触发导航事件

还有一个支持链接,它是一个 mailto://链接并打开一个 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();
}
}

关于c# - 如何在用户单击 Web 浏览器中的链接时进行拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8800764/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com