jya*_*ard 7 c# silverlight windows-phone-7
我试图拦截点击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")
{
}
Run Code Online (Sandbox Code Playgroud)
但是当我读取Uri的一些属性时,我认为这是一个异常,当它是带有默认文件的标准Uri时:// URL此外,对于以shared://开头的链接,甚至不会触发导航事件
现在我能够捕获一个共享://我不在乎,但至少我希望能够检索我们要导航到的URL,并取消默认操作特定的URL.
有什么想法发生了什么?谢谢
编辑:事实证明,问题是只为以下链接生成导航事件:file://,http://或mailto:// Uri的scheme属性仅适用于http://和mailto://链接
所以我最后做的是用http:// shared/blah替换shared://链接......我看看这个URL ......这对我有用.我现在可以拥有具有不同操作的链接(比如打开一个额外的窗口),具体取决于html中的链接.
这是我的最终代码,以防将来对某人有用:
对于about屏幕,我使用WebBrowser组件中显示的html文件.关于页面有一个"告诉你的朋友关于这个应用程序"链接以及外部网站的链接.它还有本地子页面.
本地子页面使用file://链接链接.这些可以在WebBrowser组件中导航.使用Internet Explorer在外部打开外部链接.告诉你的朋友链接是由http://共享链接组成的,它会打开一个包含预设主题和正文的电子邮件.不幸的是,除了标准方案之外没有其他方案可用,因为它们不会触发导航事件
还有一个支持链接,它是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();
}
}
Run Code Online (Sandbox Code Playgroud)