【问题标题】:Hyperlink Label text is not showing mouse Hand cursor after hovering on it in Xamarin超链接标签文本在 Xamarin 中悬停后未显示鼠标手形光标
【发布时间】:2018-11-20 19:19:58
【问题描述】:

我正在使用 CustomRenderer 将标签文本作为超链接。我的代码标签显示带下划线,我们正在使用 Label.GestureRecognizers 来捕获点击。但是鼠标光标悬停在超链接文本上后没有显示手。

下面是我们正在使用的代码:

共享项目:

  • HyperlinkLabel.cs:
namespace HyperlinkLabelControl
{
     public class HyperLinkLabel : Label
     {
     }
}
  • MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:HyperlinkLabelControl"
             x:Class="HyperlinkLabelControl.MainPage">
    <ContentPage.Content>
        <StackLayout>
            <local:HyperlinkLabel Text="MyHyperLinkLabel" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer
                        Command="{Binding BindingContext.MyClickedCommand, Source={x:Reference List}}"
                        CommandParameter="{Binding .}" />
                </Label.GestureRecognizers>
                </local:HyperlinkLabel>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

UWP 项目:

  • HyperLinkLabelRenderer.cs:
[assembly: ExportRenderer(typeof(HyperLinkLabel), typeof(HyperLinkLabelRenderer))]
namespace HyperlinkLabelControl.UWP.Renderers
{
    public class HyperLinkLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
                Control.TextDecorations = TextDecorations.Underline;
        }
    }
}

请提供鼠标光标在 Xamarin 中悬停在超链接文本上后显示手的方法。

【问题讨论】:

    标签: xaml xamarin xamarin.forms uwp


    【解决方案1】:

    超链接标签文本在 Xamarin 中悬停后未显示鼠标手形光标

    问题是Hyperlink 没有传递给TextBlock.Inlines。而Underline 枚举仅在行文本样式下提供。您可以在HyperLinkLabelRenderer 类中添加Hyperlink,如下所示。

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            Control.Inlines.Clear();
            var hl = new Hyperlink();
            hl.Inlines.Add(new Run { Text = Element.Text });
            Control.Inlines.Add(hl);
        }
    }
    

    【讨论】:

    • 谢谢@Nico Zhu,它对我有用。还有一件事我可以复制这个超链接标签的文本吗?
    • 复制文本?哪个文本?网址还是显示文字?
    • 你可以使用这个属性Control.IsTextSelectionEnabled = true
    • 朱:我要复制Label的Display Text。属性 Control.IsTextSelectionEnabled 不允许我选择标签文本。
    • 还有一件事,处理文本超链接上的 Clicked 事件的最佳方法是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多