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