在 HTML5 中,我可以通过执行以下操作来完成此操作:
lblTxt.Text = "For more info <a href='http://url.com1'>click here</a>, for contact <a href='mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8f8382988d8f98ac8883818d8582c28f8381" rel="noreferrer noopener nofollow">[email protected]</a>'>send an email</a>";
谢谢
最佳答案
由于其结构与 HTML 根本不同,链接在 Xamarin.Forms 中并不是一等公民。
如果您坚持使用链接,您可以创建一个 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
创建一个自定义 View ,并使用 Text
属性来分解文本并动态添加标签。这在技术上是可行的。
但是:如果你能无论如何避免的话,请不要这样做。带有超链接的文本不是原生 UI 习惯用法。为了获得最佳的用户体验,您应该坚持使用经过验证的 native UI 习惯用法。如果文本不够大,链接可能很难点击,而且老实说,这很可能不是实现您想要的效果的最佳选择。
关于xaml - 如何在 Xamarin Forms 中超链接部分标签文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49067437/