You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI Windows平台:如何让控件悬停时显示指针光标?

Windows平台MAUI实现控件悬停显示指针光标

可行,通过调用Windows原生WinUI的API即可实现鼠标悬停时显示指针样式光标,以下是具体实现方案:

方法一:直接使用Pointer事件处理

XAML代码

给目标Label/Button添加PointerGestureRecognizer,绑定指针进入和退出事件:

<!-- Label示例 -->
<Label Text="点击触发操作">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding YourTapCommand}" />
        <PointerGestureRecognizer PointerEntered="OnPointerEntered" 
                                  PointerExited="OnPointerExited" />
    </Label.GestureRecognizers>
</Label>

<!-- Button示例 -->
<Button Text="点击按钮">
    <Button.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding YourTapCommand}" />
        <PointerGestureRecognizer PointerEntered="OnPointerEntered" 
                                  PointerExited="OnPointerExited" />
    </Button.GestureRecognizers>
</Button>

C#后台代码

在事件处理方法中获取Windows原生控件实例,设置光标样式:

private void OnPointerEntered(object? sender, PointerEventArgs e)
{
    if (sender is View control && control.Handler?.PlatformView is Microsoft.UI.Xaml.FrameworkElement winuiElement)
    {
        // 设置为手型指针
        winuiElement.Cursor = Microsoft.UI.Xaml.Input.InputSystemCursor.Create(
            Microsoft.UI.Xaml.Input.InputSystemCursorShape.Hand);
    }
}

private void OnPointerExited(object? sender, PointerEventArgs e)
{
    if (sender is View control && control.Handler?.PlatformView is Microsoft.UI.Xaml.FrameworkElement winuiElement)
    {
        // 恢复默认箭头光标
        winuiElement.Cursor = Microsoft.UI.Xaml.Input.InputSystemCursor.Create(
            Microsoft.UI.Xaml.Input.InputSystemCursorShape.Arrow);
    }
}

方法二:封装为Behavior复用(推荐)

如果多个控件需要该功能,可封装成Behavior,避免重复编写事件逻辑:

Behavior代码

using Microsoft.Maui.Controls;
using Microsoft.UI.Xaml.Input;

public class HoverPointerBehavior : Behavior<View>
{
    protected override void OnAttachedTo(View bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.PointerEntered += OnPointerEntered;
        bindable.PointerExited += OnPointerExited;
    }

    protected override void OnDetachingFrom(View bindable)
    {
        base.OnDetachingFrom(bindable);
        bindable.PointerEntered -= OnPointerEntered;
        bindable.PointerExited -= OnPointerExited;
    }

    private void OnPointerEntered(object? sender, PointerEventArgs e)
    {
        if (sender is View view && view.Handler?.PlatformView is Microsoft.UI.Xaml.FrameworkElement element)
        {
            element.Cursor = InputSystemCursor.Create(InputSystemCursorShape.Hand);
        }
    }

    private void OnPointerExited(object? sender, PointerEventArgs e)
    {
        if (sender is View view && view.Handler?.PlatformView is Microsoft.UI.Xaml.FrameworkElement element)
        {
            element.Cursor = InputSystemCursor.Create(InputSystemCursorShape.Arrow);
        }
    }
}

XAML使用示例

<Label Text="点击我">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding TapCommand}" />
    </Label.GestureRecognizers>
    <Label.Behaviors>
        <local:HoverPointerBehavior />
    </Label.Behaviors>
</Label>

注:Button控件在Windows平台默认悬停时会显示指针光标,若自定义样式导致默认行为失效,可使用上述方法强制设置。

内容的提问来源于stack exchange,提问作者Frank Mehlhop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:50:31