首页 > 解决方案 > Xamarin.Forms 标签中的超链接

问题描述

如果它仅显示 1 个超链接,则此方法可以正常工作,但出现多个超链接呈现的问题并且出现以下问题

BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in <036ea626158e48a4b8dcc52d0593c6a6>:0 at Xamarin.Forms.BindingExpression+BindingExpressionPart.b__49_0 () [0x00000] in <036ea626158e48a4b8dcc52d0593c6a6>:0 at Java.Lang.Thread+RunnableImplementor.Run ( ) [0x00008] in :0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in :0 at (wrapper dynamic-method) System.Object.18(intptr,intptr) - ------------------ --------- 消息 --------- 索引并且长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ------------------- BindingExpression+BindingExpressionPart.b__49_0 () [0x00000] in <036ea626158e48a4b8dcc52d0593c6a6>:0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System. IntPtr native__this) [0x00009] in :0 at (wrapper dynamic-method) System.Object.18(intptr,intptr) ------------------------ -------- --------- 消息 --------- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ------------------- BindingExpression+BindingExpressionPart.b__49_0 () [0x00000] in <036ea626158e48a4b8dcc52d0593c6a6>:0 at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0 at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System. IntPtr native__this) [0x00009] in :0 at (wrapper dynamic-method) System.Object.18(intptr,intptr) ------------------------ -------- --------- 消息 --------- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ------------------- IntPtr native__this) [0x00009] in :0 at (wrapper dynamic-method) System.Object.18(intptr,intptr) ------------------------ -------- --------- 消息 --------- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ------------------- IntPtr native__this) [0x00009] in :0 at (wrapper dynamic-method) System.Object.18(intptr,intptr) ------------------------ -------- --------- 消息 --------- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib -------------------

HtmlLabel转换器

添加以下 HtmlLabelConverter。这支持文本正文中的多个链接,但不支持任何其他 HTML 元素。

public class HtmlLabelConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var formatted = new FormattedString();

        foreach (var item in ProcessString((string)value))
            formatted.Spans.Add(CreateSpan(item));

        return formatted;
    }

    private Span CreateSpan(StringSection section)
    {
        var span = new Span()
        {
            Text = section.Text
        };

        if (!string.IsNullOrEmpty(section.Link))
        {
            span.GestureRecognizers.Add(new TapGestureRecognizer()
            {
                Command = _navigationCommand,
                CommandParameter = section.Link
            });
            span.TextColor = Color.Blue;
        }

        return span;
    }

    public IList<StringSection> ProcessString(string rawText)
    {
        const string spanPattern = @"(<a.*?>.*?</a>)";

        MatchCollection collection = Regex.Matches(rawText, spanPattern, RegexOptions.Singleline);

        var sections = new List<StringSection>();

        var lastIndex = 0;

        foreach (Match item in collection)
        {
            *****sections.Add(new StringSection() { Text = rawText.Substring(lastIndex, item.Index) });*****  <!--Here the issue occurs -->
            *****lastIndex += item.Index + item.Length;*****  <!--Here the issue occurs -->

            // Get HTML href 
            var html = new StringSection()
            {
                Link = Regex.Match(item.Value, "(?<=href=\\\")[\\S]+(?=\\\")").Value,
                Text = Regex.Replace(item.Value, "<.*?>", string.Empty)
            };

            sections.Add(html);
        }

        ***sections.Add(new StringSection() { Text = rawText.Substring(lastIndex) });***   <!--Here the issue occurs -->

        return sections;
    }

    public class StringSection
    {
        public string Text { get; set; }
        public string Link { get; set; }
    }

     private ICommand _navigationCommand = new Command<string>((url) =>
     {
         Device.OpenUri(new Uri(url));
     });

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
         throw new NotImplementedException();
     }
}

然后是 XAML,我会这样称呼它。

<ContentPage.Resources>
<ResourceDictionary>
    <local:HtmlLabelConverter x:Key="HtmlLabelConverter" />
</ResourceDictionary>

标签: c#xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


推荐阅读