首页 > 解决方案 > Xamarin.Forms 标签中的超链接 - 索引超出范围

问题描述

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

在 <036ea626158e48a4b8dcc52d0593c6a6>:0 中应用 (System.Boolean fromTarget) [0x0003e] 在 Xamarin.Forms.BindingExpression+BindingExpressionPart.b__49_0 () [0x00000] 在 <036ea626158e48a4b8dcc52d0593c6anable>:0 中的 Xamarin.Forms.BindingExpression+BindingExpressionPart.b__49_0 () [0x00000]。 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 ---------- 036ea626158e48a4b8dcc52d0593c6a6>:0 在 Java.Lang.Thread+RunnableImplementor.Run () [0x00008] 在 :0 在 Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] 在 :0 在 (wrapper动态方法) System.Object.18(intptr,intptr) ------------------------------- ----- ---- 消息 --------- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ---------- 036ea626158e48a4b8dcc52d0593c6a6>:0 在 Java.Lang.Thread+RunnableImplementor.Run () [0x00008] 在 :0 在 Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] 在 :0 在 (wrapper动态方法) System.Object.18(intptr,intptr) ------------------------------- ----- ---- 消息 --------- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ---------- intptr) ------------------- --------- 消息 -------- -- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ---------- intptr) ------------------- --------- 消息 -------- -- 索引和长度必须引用字符串中的位置。参数名称:长度------------------- --------- 来源----- ---- mscorlib ----------

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; }
}

标签: c#.netxamarinxamarin.formssubstring

解决方案


我已经解决了这个问题,rawText.Substring index out of range..

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

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

                var sections = new List<StringSection>();

                int lastIndex = 0;
                int lastLinkIndex = 0;

                foreach (Match item in collection)
                {
                    var foundText = item.Value;
                    var currentIndexText = rawText.Substring(lastIndex, (item.Index - lastIndex));
                    sections.Add(new StringSection() { Text = currentIndexText });
                    lastIndex += item.Index + item.Length;
                    lastLinkIndex = item.Index + item.Length;
                    // 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(lastLinkIndex) });

                return sections;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

推荐阅读