Гиперссылка в ярлыке Xamarin.Forms - индекс вне допустимого диапазона - PullRequest
0 голосов
/ 06 мая 2020

Это отлично работает, если присутствует только 1 гиперссылка, но возникает проблема, отображается более одной гиперссылки, и возникла проблема ниже

--------- Трассировка стека --------- в System.String.Substring (System.Int32 startIndex, длина System.Int32) [0x0004c] в: 0 в MPI.UI.Mobile.Converter.HtmlLabelConverter.ProcessString (System.String rawText) [0x0004b] в: 0 в MPI.UI.Mobile.Converter.HtmlLabelConverter.Convert (значение System.Object, System.Type targetType, параметр System.Object, культура System.Globalization.CultureInfo) [0x00008] в: 0 в Xamarin.Forms.Binding.GetSourceValue (значение System.Object, System.Type targetPropertyType) [0x0001b] в <036ea626158e48a4b8dcc52d0593c6a6>: 0 в Xamarin.Forms.BindingExpression.Appormly.Core, источник Xamarin.Forms.BindingExpression.Appormly.Core (System.Obarin. Forms.BindableProperty свойство, System.Boolean fromTarget) [0x001ce] в <036ea626158e48a4b8dcc52d0593c6a6>: 0 в Xamarin.Forms.BindingExpression.Apply (System.Boolean fr omTarget) [0x0003e] в <036ea626158e48a4b8dcc52d0593c6a6>: 0 в Xamarin.Forms.BindingExpression + BindingExpressionPart.b__49_0 () [0x00000] в <036ea626158e48a4b8dcc652d0): [Run]. : 0 в Java .Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] в: 0 в (динамическая оболочка c -метод) System.Object.18 (intptr, intptr) - ------------------------------ --------- Сообщение --------- Указатель а длина должна относиться к месту в строке. Имя параметра: длина ------------------------------- --------- Источник ----- ---- mscorlib ---------- </p>

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

1 Ответ

1 голос
/ 07 мая 2020

Я исправил эту проблему, проблема с rawText.Substring индекс вне допустимого диапазона ..

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;
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...