c# - 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>
解决方案
推荐阅读
- google-cloud-platform - Odoo 13 配置workers和limit_memory_hard、limit_memory_soft、max_cron_threads
- flutter - 管理页面的最佳位置在哪里?
- flutter - 如果我只使用提供 100 个功能的插件中的 1 个功能,它会影响颤振应用程序的大小吗?
- c# - 除非我注销用户,否则 CollectionView 不会更新
- maven - 在 Maven 阴影 jar 中包含测试类
- ios - Flutter Project Xcode 文件损坏
- android - 从另一个手机上的另一个应用程序控制应用程序android
- python - 可变x轴间隙matplotlib
- python - 熊猫得到过时的年份列
- html - 如何将 FlexBox 用于响应式网格