简体   繁体   English

如何在 Xamarin Forms 中超链接部分标签文本?

[英]How to hyperlink part of a label text in Xamarin Forms?

In HTML5 I could accomplish this by doing something like:在 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>";

Tks塔克斯

Links are no first class citizen in Xamarin.Forms, due to its structure which is fundamentally different to HTML.链接在 Xamarin.Forms 中不是一等公民,因为它的结构与 HTML 根本不同。

You could - if you insisted on links - create a StackLayout containing each one label for the text before and after the link and one for the link你可以 - 如果你坚持链接 - 创建一个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>

Admittedly this does not scale very well, but it's conceivable to create a custom view based on a ContentView with a Text property that decomposes the text and adds the labels dynamically.诚然,这不能很好地扩展,但可以想象基于ContentView创建自定义视图,该视图具有Text属性,可分解文本并动态添加标签。 This would work - technically.这会起作用 - 从技术上讲。

But : Please don't do it, if you can avoid it at any rate.但是:请不要这样做,如果您无论如何都可以避免的话。 Texts with hypeterlinks are no native UI idiom.带有超链接的文本不是原生的 UI 习惯用法。 For the best UX you should stick to proven native UI idioms.为了获得最佳 UX,您应该坚持使用经过验证的原生 UI 习惯用法。 Links may be hard to hit if the text is not large enough and - honestly - are most likely not the most beautiful option to do what you want.如果文本不够大,链接可能很难点击,而且 - 老实说 - 很可能不是做你想做的最漂亮的选择。

XAML Approach: 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>

Code Behind Approach:方法背后的代码:

XAML 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>

Code Behind背后的代码

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    
}

With your ideas I ended up with this code:根据您的想法,我最终得到了以下代码:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM