如何在 MacOS and/or UWP 的 Xamarin.Forms 按钮上实现悬停事件?

How can I implement a hover event on a Xamarin.Forms button for MacOS and/or UWP?

我有一个针对 MacOS 和 WPF 以及其他平台的 Xamarin.Forms 项目。对于 MacOS nad UWP,我需要知道鼠标何时悬停在按钮上,以便向用户显示工具提示。但是默认的 Xamarin.Forms 按钮 doesn't contain a MouseOver 或类似的东西。我知道这不是移动设备的优先事项,但有没有办法为 UWP 和 MacOS 实现这一点?

我是否必须使用自定义渲染器?如果是,是否有人有 MacOS/UWP 的自定义渲染器示例,可以让我知道鼠标何时悬停在按钮上?

How can I implement a hover event on a Xamarin.Forms button for UWP?

根据您的要求,您可以使用 Effect 来实现此功能。 首先我们需要 make MouseOverEffect 并添加 MouseOver Action type attach 属性.

    public static class MouseOverEffect
    {
        public static readonly BindableProperty MouseOverProperty =
    BindableProperty.CreateAttached("MouseOver", typeof(Action), typeof(MouseOverEffect), default(Action), propertyChanged: OnhandlerChanged);
    
    
        public static Action GetMouseOver(BindableObject view)
        {
            return (Action)view.GetValue(MouseOverProperty);
        }
    
    
        public static void SetMouseOver(BindableObject view, Action value)
        {
            view.SetValue(MouseOverProperty, value);
        }
    
        static void OnhandlerChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var view = bindable as View;
            if (view == null)
            {
                return;
            }
    
            Action action = (Action)newValue;
            if (action != null)
            {
                view.Effects.Add(new ControlTooltipEffect());
            }
            else
            {
                var toRemove = view.Effects.FirstOrDefault(e => e is ControlTooltipEffect);
                if (toRemove != null)
                {
                    view.Effects.Remove(toRemove);
                }
            }
        }
    
        class ControlTooltipEffect : RoutingEffect
        {
            public ControlTooltipEffect() : base($"Microsoft.{nameof(MouseOverEffect)}")
            {
    
            }
        }
    }

然后制作IWP客户端Effect实现

获取附加按钮控件并监听 PointerEntered 甚至,当指针进入时调用操作方法。

    [assembly: ResolutionGroupName("Microsoft")]
    [assembly: ExportEffect(typeof(UWPMouseEffect), nameof(MouseOverEffect))]
    namespace NavigateTest.UWP
    {
        public class UWPMouseEffect : PlatformEffect
        {
            protected override void OnAttached()
            {
                var control = Control ?? Container;
    
                if (control is FormsButton)
                {
                    control.PointerEntered += Control_PointerEntered;
                    
                }
            }
    
            private void Control_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
            {
               var action =  MouseOverEffect.GetMouseOver(Element);
                action();
            }
    
            protected override void OnDetached()
            {
                
            }
        }
    }

用法

    <Button
        effect:MouseOverEffect.MouseOver="{Binding MouseOverAction}"
        Clicked="Button_Clicked"
        Text="Next"
        />

ViewModel

    public class Page2ViewModel : INotifyPropertyChanged, IDisposable
    {
        public Page2ViewModel()
        {
            MouseOver(() =>
            {
            // do some sth
    
            });
        }
        public Action MouseOverAction { set; get; }
        public void MouseOver(Action action)
        {
            MouseOverAction = action;
        }
    
    }