【问题标题】:Detect is a website address or email address on textblock检测是文本块上的网站地址或电子邮件地址
【发布时间】:2016-11-25 07:53:18
【问题描述】:

我有一个 TextBlock,其数据来自 JSON。我想如果文本阻止网站地址或电子邮件,文本颜色变为蓝色并且用户可以单击(如果电子邮件地址将转到电子邮件应用程序,用户可以直接向该地址写电子邮件。同时,如果网址,它将立即打开网络浏览器)。 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 的 JSON 数据示例:

如何应用它?

【问题讨论】:

  • Stanislav Kniazev 的回答应该会有所帮助。 stackoverflow.com/questions/2092890/…
  • 它不是 wpf,而是在 uwp 上
  • 属性 Deskripsi 下是什么?我想你可以看看this answer
  • Deskripsi 取自 JSON 对象 post_clean 因此文本块描述将从 JSON 对象 post_clean 检索数据。如果里面有链接或者邮箱,那么文字颜色会变成蓝色,用户可以点击(如果邮箱地址会去邮件应用,用户可以直接写邮件到这个地址。同时,如果是网址,会立即打开网页浏览器)
  • @Rose UWP TextBlock 具有 Inlines 属性。答案的编辑部分中的代码不起作用吗?

标签: c# json uwp textblock


【解决方案1】:

我对@9​​87654321@ 做了一些修改,现在它处理绑定的字符串,搜索网站和电子邮件地址。一旦找到,它就会创建一个超链接,该链接应该会触发电子邮件应用程序或网络浏览器。

TextBlock 扩展代码:

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中的代码:

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

The working sample you will find at my Github - 我已经用你的 json 对其进行了测试,它看起来/工作得很好:

【讨论】:

  • JSON 的数据不只是一个,而是取自链接 http://..../mobileapp/GetPostByCategoryXMLa?term_id=372 用户在 gridview 上选择项目,然后项目显示为标题、图片和说明在第 2 页。
  • @Rose 我以为你的问题是关于检测电子邮件/网站地址。
  • 是的,我的问题是关于检测电子邮件/网站地址。 Textblock 是来自带有链接 http://..../mobileapp/GetPostByCategoryXMLa?term_id=372 的 JSON 数据的数据绑定,而 JSON 的数据不仅仅是一个。我试图应用上面的代码,但没有检测到它们。在第一页有一个用户可以选择的gridview。一旦用户选择,将在 page2 上显示一个文本块的描述
  • 你试过上面的例子吗?如果您绑定文本 - 它是否正确显示?
  • 我试过了,但是检测不到。持续存在 \r \n 并且未检测到网站地址和电子邮件地址
猜你喜欢
  • 2013-05-13
  • 1970-01-01
  • 2015-02-16
  • 2017-07-23
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
相关资源
最近更新 更多