以下代码不起作用:
var fs = new FormattedString();
fs.Spans.Add(new Span { Text = "\n\n" });
var telSpan = new Span { Text = "Hi Mum!" };
telSpan.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() =>
{
// ...
})
});
fs.Spans.Add(telSpan);
如果我将 TapGestureRecognizer 放在父级 Label 上,则可以正常启动。
我在XAML中声明了在其他地方带有 GestureRecognizer 的Span,并且工作正常。
这有什么问题吗?
答案 0 :(得分:0)
向标签中的每个跨度添加 TapGestureRecognizers。在本例中,我将 Label 添加到 StackLayout。
var fs = new FormattedString();
foreach(var sentence in paragraph)
{
var span = new Span
{
Text = sentence.Message,
TextColor = Color.FromHex("#222"),
FontSize = 16,
};
span.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(i =>
{
// On double tap the sentence Id will be written to the console.
Console.WriteLine(i);
}),
CommandParameter = sentence.Id,
NumberOfTapsRequired = 2 // removing this line will default number of taps to 1
});
fs.Spans.Add(span);
}
// remove LineBreakMode = LineBreakMode.WordWrap if unnecessary.
someStackLayout.Children.Add(new Label { LineBreakMode = LineBreakMode.WordWrap, FormattedText = fs });