在文本块上检测到是网站地址还是电子邮件地址 [英] Detect is a website address or email address on textblock

查看:110
本文介绍了在文本块上检测到是网站地址还是电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextBlock,其数据来自JSON.我想如果该文本块是网站地址或电子邮件,则文本颜色变为蓝色,并且用户可以单击(如果该电子邮件地址将转到电子邮件应用程序,并且用户可以直接向该地址写一封电子邮件.网站地址,它将立即打开网络浏览器). XAML:

I have a TextBlock whose data comes from JSON. I would like if the textblock the website address or email, the text color becomes blue and the user can click (if the email address it will go to the email application and the user can write an email directly to this address. Meanwhile, if the website address, it will immediately open the web browser). XAML:

<TextBlock x:Name="DetailDeskripsi" Width="290" Text="{Binding Deskripsi}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="auto" TextWrapping="Wrap" FontSize="15" TextAlignment="Justify" Foreground="#FFCA6402"/>

来自 http://.../mobileapp/GetPostByCategoryXMLa?term_id = 378 :

如何应用?

推荐答案

我从这里修改了答案,现在它处理绑定的字符串,搜索网站和电子邮件地址.找到一个链接后,它将创建一个超链接,该超链接应会触发电子邮件应用程序或Web浏览器.

I've modified a little the answer from here and now it processes the bound string, searching for website and e-mail adresses. Once it founds one, it creates a hyperlink which should fire e-mail app or webbrowser.

TextBlock扩展的代码:

The code for TextBlock extendion:

public static class TextBlockExtension
{
    public static string GetFormattedText(DependencyObject obj)
    { return (string)obj.GetValue(FormattedTextProperty); }

    public static void SetFormattedText(DependencyObject obj, string value)
    { obj.SetValue(FormattedTextProperty, value); }

    public static readonly DependencyProperty FormattedTextProperty =
        DependencyProperty.Register("FormattedText", typeof(string), typeof(TextBlockExtension),
        new PropertyMetadata(string.Empty, (sender, e) =>
        {
            string text = e.NewValue as string;
            var textBl = sender as TextBlock;
            if (textBl != null && !string.IsNullOrWhiteSpace(text))
            {
                textBl.Inlines.Clear();
                Regex regx = new Regex(@"(http(s)?://[\S]+|www.[\S]+|[\S]+@[\S]+)", RegexOptions.IgnoreCase);
                Regex isWWW = new Regex(@"(http[s]?://[\S]+|www.[\S]+)");
                Regex isEmail = new Regex(@"[\S]+@[\S]+");
                foreach (var item in regx.Split(text))
                {
                    if (isWWW.IsMatch(item))
                    {
                        Hyperlink link = new Hyperlink { NavigateUri = new Uri(item.ToLower().StartsWith("http") ? item : $"http://{item}"), Foreground = Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush };
                        link.Inlines.Add(new Run { Text = item });
                        textBl.Inlines.Add(link);
                    }
                    else if (isEmail.IsMatch(item))
                    {
                        Hyperlink link = new Hyperlink { NavigateUri = new Uri($"mailto:{item}"), Foreground = Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush };
                        link.Inlines.Add(new Run { Text = item });
                        textBl.Inlines.Add(link);
                    }
                    else textBl.Inlines.Add(new Run { Text = item });
                }
            }
        }));
}

还有xaml中的代码:

And the code in xaml:

<TextBlock extension:TextBlockExtension.FormattedText="{x:Bind TextToFormat, Mode=OneWay}" FontSize="15" Margin="10" TextWrapping="WrapWholeWords"/>

您将在我的Github上找到的工作示例-我已经用您的json测试了它,它看起来/工作得很好:

The working sample you will find at my Github - I've tested it with your json and it looks/works quite nice:

这篇关于在文本块上检测到是网站地址还是电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆