c# - 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; }
}
解决方案
我已经解决了这个问题,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;
}
}
推荐阅读
- c++ - 从 QML 调用 QAbstractTableModel setData 方法
- javascript - 在Vue中的网格中单击特定项目时添加类
- java - 无法从队列创建热流
- java - SpringBoot 不在 MySQL Workbench 上创建表
- sql - T SQL 累加减法
- python - 如何使用 boto3 资源更新 EC2 实例状态
- postgresql - 如何在 PostGIS 中将 st_Line_Locate_Point() 与 MULTILINESTRING 转换一起使用?
- excel - 将附件添加到电子邮件
- react-select - 我可以自定义 react-select Creatable 以不显示创建选项吗?
- python - 使用 Python 中的 ElementTree 解析 XML 中的 xsi:type