【发布时间】:2020-08-20 22:46:47
【问题描述】:
如果它仅显示 1 个超链接,则此方法可以正常工作,但出现多个超链接呈现的问题并出现以下问题
--------- 堆栈跟踪 --------- 在 System.String.Substring (System.Int32 startIndex, System.Int32 长度) [0x0004c] in :0 at MPI.UI.Mobile.Converter.HtmlLabelConverter.ProcessString (System.String rawText) [0x0004b] in :0 at MPI.UI.Mobile.Converter.HtmlLabelConverter.Convert (System.Object 值,System.Type 目标类型,System.Object 参数, System.Globalization.CultureInfo 文化)[0x00008] in :0 at Xamarin.Forms.Binding.GetSourceValue(System.Object 值,System.Type targetPropertyType) [0x0001b] 在 :0 在 Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject,Xamarin.Forms.BindableObject 目标, Xamarin.Forms.BindableProperty 属性,System.Boolean fromTarget) [0x001ce] 在 :0 处 Xamarin.Forms.BindingExpression.Apply(System.Boolean fromTarget) [0x0003e] 在 :0 处 Xamarin.Forms.BindingExpression+BindingExpressionPart.b__49_0 () [0x00000] 在 :0 处 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 ----------
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; }
}
【问题讨论】:
-
您是否要从字符串中提取 URL?
-
@Anand 是的,实际上是在尝试从字符串中提取 URL 和文本
标签: c# .net xamarin xamarin.forms substring