在HTML5中,我可以通过执行以下操作来实现此目的:
lblTxt.Text = "For more info <a href='http://url.com1'>click here</a>, for contact <a href='mailto:contact@domain.com'>send an email</a>";
韩国社交协会
答案 0 :(得分:1)
链接在Xamarin.Forms中不是一等公民,因为它的结构与HTML根本不同。
你可以 - 如果你坚持使用链接 - 创建一个StackLayout
,其中包含链接前后文本的每个标签,以及链接的标签
<ContentPage ...>
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="LinkLabel" TargetType="Label">
<Setter Property="TextColor" Value="Blue" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Orientation="Horizontal">
<Label Text="For more info" />
<Label Text="click here" Style="{StaticResource LinkLabel}">
<Label.GestureRecognizers>
<!-- Add TapGestureRecognizer that invokes an action on your viewmodel -->
</Label.GestureRecognizers>
</Label>
<Label Text=", for contact">
<!-- and so on, I think you#ve got the gist -->
</StackLayout>
</ContentPage>
不可否认,这不能很好地扩展,但可以设想基于具有ContentView
属性的Text
创建自定义视图,该属性可以分解文本并动态添加标签。这在技术上是有效的。
但是:如果你能以任何速度避免它,请不要这样做。具有超链接的文本不是本机UI惯用语。对于最好的UX,您应该坚持使用经过验证的原生用户界面习语。如果文字不够大,那么链接可能很难被击中 - 老实说 - 很可能不是做你想做的最美好的选择。
答案 1 :(得分:0)
根据您的想法,我最终得到了这段代码:
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="long bla bla bla..., "/>
<Span Text="click here" ForegroundColor="Blue"/>
<Span Text=" and more bla bla"/>
</FormattedString>
</Label.FormattedText>
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ClickHereCommand}" NumberOfTapsRequired="1" />
</Label.GestureRecognizers>
</Label>
答案 2 :(得分:0)
XAML方法:
<Label
x:Name="LDescription"
HorizontalOptions="FillAndExpand"
TextColor="#000000">
<Label.FormattedText>
<FormattedString>
<Span Text="We have so much to offer."/>
<Span Text=" "/>
<Span Text="Learn More." TextColor="#CF9F24">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Command}" NumberOfTapsRequired="1"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
方法背后的代码:
XAML
<Label
x:Name="LDescription"
HorizontalOptions="FillAndExpand"
TextColor="#000000">
<Label.FormattedText>
<FormattedString>
<Span Text="We have so much to offer."/>
<Span Text=" "/>
<Span x:Name="SLearnMore" Text="Learn More." TextColor="#CF9F24"/>
</FormattedString>
</Label.FormattedText>
</Label>
隐藏代码
YourPage() //constructor
{
Appearing += YourPage_Appearing;
Disappearing += YourPage_Disappearing;
}
void YourPage_Appearing(object sender, EventArgs e)
{
var learnMore = new TapGestureRecognizer();
learnMore.Tapped += LearnMore_Tapped;
SLearnMore.GestureRecognizers.Add(learnMore);
}
void YourPage_Disappearing(object sender EventArgs e)
{
SLearnMore.GestureRecognizers.Clear();
}
void LearnMore_Tapped(object sender, EventArgs e)
{
// Your click logic
}