悬停在超链接上时,如何在Xamarin.forms中更改鼠标光标?

时间:2018-10-30 15:57:37

标签: xamarin xamarin.forms uwp mouseover mouse-cursor

Xamarin.forms 3.3.0 update中,建议通过以下方式创建超链接:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="This app is written in C#, XAML, and native APIs using the" />
                <Span Text=" " />
                <Span Text="Xamarin Platform" FontAttributes="Bold" TextColor="Blue" TextDecorations="Underline">
                    <Span.GestureRecognizers>
                       <TapGestureRecognizer 
                            Command="{Binding TapCommand, Mode=OneWay}"
                            CommandParameter="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/"/>
                     </Span.GestureRecognizers>
                </Span>
                <Span Text="." />
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
</Label>

通常,在Windows上,当鼠标悬停在超链接上时,鼠标光标会更改。 Xamarin.forms中是否有办法获得与鼠标定距相同的更改?

1 个答案:

答案 0 :(得分:1)

我认为您可以为UWP创建自定义渲染器。 例如,如下所示:

[assembly: ExportRenderer(typeof(HyperLinkLabel), typeof(HyperLinkLabel_UWP))]
namespace MyApp.UWP.CustomRenders
{
    public class HyperLinkLabel_UWP: LabelRenderer
    {
        private readonly Windows.UI.Core.CoreCursor OrigHandCursor = Window.Current.CoreWindow.PointerCursor;

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                Control.PointerExited += Control_PointerExited;
                Control.PointerMoved += Control_PointerMoved;
            }
        }

        private void Control_PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            Windows.UI.Core.CoreCursor handCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 1);
            if (handCursor != null)
                Window.Current.CoreWindow.PointerCursor = handCursor;
        }

        private void Control_PointerExited(object sender,     Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if (OrigHandCursor != null)
                Window.Current.CoreWindow.PointerCursor = OrigHandCursor;
        }
    }
}
相关问题