xamarinxamarin.formsuwpmouseovermouse-cursor

How do I change the mouse cursor in Xamarin.forms when hovering over a hyperlink?


In the Xamarin.forms 3.3.0 update there's a suggestion to create hyperlinks via:

<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://learn.microsoft.com/en-us/xamarin/xamarin-forms/"/>
                     </Span.GestureRecognizers>
                </Span>
                <Span Text="." />
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
</Label>

Normally, on Windows the mouse cursor changes when hovering over a hyperlink. Is there a way in Xamarin.forms to get the same change of mouse courser?


Solution

  • I think that you could create a custom renderer for UWP. For example, something like this:

    [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;
            }
        }
    }