pointer Enter on desktop doesn't change my cursor the first time

Eduardo Gomez Romero 1,355 Reputation points
2024-04-06T02:17:21.6+00:00

https://reccloud.com/u/fiuqu7p

   <Label
       x:Name="ContactLbl"
       Grid.RowSpan="2"
       Grid.Column="1"
       FontFamily="Mat"
       FontSize="40"
       HorizontalOptions="EndAndExpand"
       Text="{x:Static helpers:IconFont.Email}"
       VerticalTextAlignment="Center">
       <Label.GestureRecognizers>
           <PointerGestureRecognizer
               PointerEnteredCommand="{Binding Path=ContactPointerEnteredCommand, Source={x:RelativeSource AncestorType={x:Type vm:MyCoursesDetailPageViewModel}}}"
               PointerEnteredCommandParameter="{Binding Source={x:Reference ContactLbl}}"
               PointerExitedCommand="{Binding Path=ContactPointerExitedCommand, Source={x:RelativeSource AncestorType={x:Type vm:MyCoursesDetailPageViewModel}}}"
               PointerExitedCommandParameter="{Binding Source={x:Reference ContactLbl}}" />
       </Label.GestureRecognizers>
   </Label>

    [RelayCommand]
    void ContactPointerEntered(Label emailLabel) {

#if WINDOWS || MACCATALYST
        emailLabel.SetCustomCursor(CursorIcon.Hand, emailLabel.Handler?.MauiContext);
#endif
        emailLabel.TextColor = Color.FromRgb(65, 105, 225);
    }

    [RelayCommand]
    void ContactPointerExited(Label emailLabel) {

        emailLabel.TextColor = Color.FromHex("#000000");
    }

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-04-08T02:36:47.32+00:00

    Hello,

    After testing, when changing icons with gestures, the issue of not changing the cursor style does occur for the first time written by Vladislav Antonyuk.

    Did you refer to the blog called Setting a cursor for .NET MAUI VisualElement to modify the mouse style?

    A Behavioral way to change the mouse style is mentioned in this Blog and it works as expected.

    In order to set a custom cursor from XAML, let's create an attached property. Create a new file in the root of the project named CursorBehavior.cs and paste the following code:

    public class CursorBehavior
    {
        public static readonly BindableProperty CursorProperty = BindableProperty.CreateAttached("Cursor", typeof(CursorIcon), typeof(CursorBehavior), CursorIcon.Arrow, propertyChanged: CursorChanged);
    
        private static void CursorChanged(BindableObject bindable, object oldvalue, object newvalue)
        {
            if (bindable is VisualElement visualElement)
            {
                visualElement.SetCustomCursor((CursorIcon)newvalue, Application.Current?.MainPage?.Handler?.MauiContext);
            }
        }
    
        public static CursorIcon GetCursor(BindableObject view) => (CursorIcon)view.GetValue(CursorProperty);
    
        public static void SetCursor(BindableObject view, CursorIcon value) => view.SetValue(CursorProperty, value);
    }
    
    

    XAML:

    <Label Text="test label" local:CursorBehavior.Cursor="Hand">
    </Label>
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.