如何在 Xamarin.UWP 应用程序中实现 LongPress?
Posted
技术标签:
【中文标题】如何在 Xamarin.UWP 应用程序中实现 LongPress?【英文标题】:How do I implement LongPress in a Xamarin.UWP application? 【发布时间】:2021-02-04 23:30:00 【问题描述】:我正在尝试实现一个自定义PlatformEffect
,当用户为我的Xamarin.UWP
长时间按住一个控件时将执行该自定义PlatformEffect
。但是我发现我的应用程序不响应鼠标点击。我读到我需要根据备注here 更改应用程序的GestureSettings
以解决此问题。问题是我不知道如何为 Xamarin.UWP 应用程序做到这一点,任何想法都会非常有帮助。
【问题讨论】:
那么,你只想用 PlatformEffect 实现鼠标按住事件,对吧? 是的,这就是我想要做的。 请查看code sample 了解如何使用Gesture
实现鼠标按住。
@NicoZhu-MSFT 谢谢你,但代码没有太大帮助。我知道如何更改 GestureSettings,问题是我不知道如何更改 FrameworkElement 或 UIElement 的 GestureSettings。我创建了一个 GestureRecognizer 对象,但从未触发持有事件。
也许添加一些代码会让我的问题更清楚?
【参考方案1】:
如何更改 Xamarin.UWP 应用程序的 GestureSettings?
源自官方document,触摸可以产生Holding动作,而鼠标设备一般不能。所以,如果要实现鼠标保持,则需要使用add GestureRecognizer
for您的元素并使用 GestureRecognizer 的持有事件来实现鼠标持有。更多详情请参考以下内容。
public static class MouseHoldingEffect
public static readonly BindableProperty MouseHoldingProperty =
BindableProperty.CreateAttached("MouseHolding", typeof(Action), typeof(MouseHoldingEffect), default(Action), propertyChanged: OnhandlerChanged);
public static Action GetMouseHolding(BindableObject view)
return (Action)view.GetValue(MouseHoldingProperty);
public static void SetMouseHolding(BindableObject view, Action value)
view.SetValue(MouseHoldingProperty, 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(MouseHoldingEffect)")
UWPMouseEffect
public class UWPMouseEffect : PlatformEffect
Windows.UI.Input.GestureRecognizer recognizer;
ManipulationInputProcessor manipulationProcessor;
protected override void OnAttached()
var control = Control ?? Container;
if (control is UIElement)
var mouseHolding = Element.GetValue(MouseHoldingEffect.MouseHoldingProperty) as Action;
var target = control as UIElement;
var parent = Window.Current.Content;
recognizer = new Windows.UI.Input.GestureRecognizer();
manipulationProcessor = new ManipulationInputProcessor(recognizer, target, parent, mouseHolding);
protected override void OnDetached()
class ManipulationInputProcessor
Action mouseHolding;
Windows.UI.Input.GestureRecognizer recognizer;
UIElement element;
UIElement reference;
TransformGroup cumulativeTransform;
MatrixTransform previousTransform;
CompositeTransform deltaTransform;
public ManipulationInputProcessor(Windows.UI.Input.GestureRecognizer gestureRecognizer, UIElement target, UIElement referenceFrame, Action holdingAction)
recognizer = gestureRecognizer;
element = target;
reference = referenceFrame;
mouseHolding = holdingAction;
// Initialize the transforms that will be used to manipulate the shape
InitializeTransforms();
// The GestureSettings property dictates what manipulation events the
// Gesture Recognizer will listen to. This will set it to a limited
// subset of these events.
recognizer.GestureSettings = GenerateDefaultSettings();
// Set up pointer event handlers. These receive input events that are used by the gesture recognizer.
element.PointerPressed += OnPointerPressed;
element.PointerMoved += OnPointerMoved;
element.PointerReleased += OnPointerReleased;
element.PointerCanceled += OnPointerCanceled;
recognizer.Holding += Recognizer_Holding;
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
recognizer.ProcessMoveEvents(e.GetIntermediatePoints(reference));
private void OnPointerCanceled(object sender, PointerRoutedEventArgs e)
recognizer.CompleteGesture();
element.ReleasePointerCapture(e.Pointer);
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
recognizer.ProcessUpEvent(e.GetCurrentPoint(reference));
// Release the pointer
element.ReleasePointerCapture(e.Pointer);
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
element.CapturePointer(e.Pointer);
// Feed the current point into the gesture recognizer as a down event
recognizer.ProcessDownEvent(e.GetCurrentPoint(reference));
private GestureSettings GenerateDefaultSettings()
return GestureSettings.HoldWithMouse;
private void Recognizer_Holding(Windows.UI.Input.GestureRecognizer sender, HoldingEventArgs args)
System.Diagnostics.Debug.WriteLine("-----------Holding---------");
mouseHolding();
private void InitializeTransforms()
cumulativeTransform = new TransformGroup();
deltaTransform = new CompositeTransform();
previousTransform = new MatrixTransform() Matrix = Matrix.Identity ;
cumulativeTransform.Children.Add(previousTransform);
cumulativeTransform.Children.Add(deltaTransform);
element.RenderTransform = cumulativeTransform;
用法
<StackLayout>
<Label
effect:MouseHoldingEffect.MouseHolding="Binding MouseHoldingAction"
FontSize="25"
Text="Hello" VerticalOptions="Center" HorizontalOptions="Center" Margin="100"/>
</StackLayout>
视图模型
public class ViewModel : INotifyPropertyChanged, IDisposable
public ViewModel()
MouseHolding(() =>
// do some stuff
);
public Action MouseHoldingAction set; get;
public event PropertyChangedEventHandler PropertyChanged;
public void Dispose()
public void MouseHolding(Action action)
MouseHoldingAction = action;
【讨论】:
谢谢,这很有帮助。 对此有一个注释,我发现这有时会产生误报。它有时会将常规轻按检测为长按(通常在轻按快速发生时)。任何处理这个问题的方法都会很棒。以上是关于如何在 Xamarin.UWP 应用程序中实现 LongPress?的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin UWP - 如何摆脱悬停在按钮上时出现的边框
如何在 xamarin.UWP 中的 Viewcell 项目选择上设置自己的颜色?
如何在 Xamarin UWP 上的 ListView 中释放内存?