phone:WebBrowser处理邮件链接

时间:2013-09-06 07:13:59

标签: c# xaml windows-phone-7 windows-phone-8 windows-phone

我有一个WebBrowser控件:

<phone:WebBrowser Name="ArticleContent" Navigating="ArticleContent_Navigating" Navigated="ArticleContent_Navigated" />

我从服务器获取文章,如HTML字符串:

string Article = "<p>Sometext</p><a href=\"mailto:artjomgsd@inbox.lv\"><span style=\"font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-font-family:&quot;Arial Unicode MS&quot;; mso-fareast-language:LV\">artjomgsd@inbox.lv</span></a>";

我这样做:

ArticleContent.NavigateToString(Article);

并使用此功能停止加载图标:

private void ArticleContent_Navigated(object sender, NavigationEventArgs e)
{
    HideLoading();
}

此函数用于处理链接(在外部浏览器中打开链接):

private void ArticleContent_Navigating(object sender, NavigatingEventArgs e)
{
    e.Cancel = true;
    WebBrowserTask webBrowserTask = new WebBrowserTask();
    webBrowserTask.Uri = new Uri(e.Uri.ToString(), UriKind.Absolute);
    webBrowserTask.Show();
}

我的问题是,为什么当我点击电子邮件超链接时没有什么好处?它甚至没有进入ArticleContent_Navigating()函数?

P.S。我想点击邮件超链接打开MailTask​​。

2 个答案:

答案 0 :(得分:0)

不幸的是,mailto: Windows Phone上的WebBrowser控件不支持它。

您可以做的是在HTML中注入Javascript,它将枚举所有a代码并汇总onclick个事件。该事件将调用window.external.Notify,而ScriptNotify将提升WebBrowser的{​​{1}}事件,并将URL作为参数。

这有点复杂,但我认为这是在Windows Phone上处理mailto协议的唯一选择。

以下是一些示例代码:

// Page Constructor
public MainPage()
{
    InitializeComponent();

    browser.IsScriptEnabled = true;
    browser.ScriptNotify += browser_ScriptNotify;
    browser.Loaded += browser_Loaded;
}

void browser_Loaded(object sender, RoutedEventArgs e)
{
    // Sample HTML code
    string html = @"<html><head></head><body><a href='mailto:test@test.com'>Send an email</a></body></html>";

    // Script that will call raise the ScriptNotify via window.external.Notify
    string notifyJS = @"<script type='text/javascript' language='javascript'>
                            window.onload = function() {
                                var links = document.getElementsByTagName('a');
                                for(var i=0;i<links.length;i++) {
                                    links[i].onclick = function() {
                                        window.external.Notify(this.href);
                                    }
                                }
                            }
                        </script>";

    // Inject the Javascript into the head section of the HTML document
    html = html.Replace("<head>", string.Format("<head>{0}{1}", Environment.NewLine, notifyJS));

    browser.NavigateToString(html);
}

void browser_ScriptNotify(object sender, NotifyEventArgs e)
{
    if (!string.IsNullOrEmpty(e.Value))
    {
        string href = e.Value.ToLower();
        if (href.StartsWith("mailto:"))
        {
            EmailComposeTask email =  new EmailComposeTask();
            email.To = href.Replace("mailto:", string.Empty);
            email.Show();
        }
    }
}

答案 1 :(得分:0)

你的Html标签问题只需在你的链接上的mailto:artjomgsd@inbox.lv \之前写 Http // 它就可以正常工作

像波纹管一样

"<p>Sometext</p><a href=\"http://mailto:artjomgsd@inbox.lv\"><span style=\"font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-font-family:&quot;Arial Unicode MS&quot;; mso-fareast-language:LV\">artjomgsd@inbox.lv</span></a>";

我现在在我的应用程序中测试了这段代码。