首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在LongPress应用程序中实现Xamarin.UWP?

如何在LongPress应用程序中实现Xamarin.UWP?
EN

Stack Overflow用户
提问于 2020-10-22 01:27:09
回答 1查看 158关注 0票数 0

我正在尝试实现一个自定义PlatformEffect,当用户为我的Xamarin.UWP按住一个控件很长一段时间时,它将被执行。但是,我发现我的应用程序不响应鼠标单击。我读到我需要根据备注GestureSettings修改应用程序的这里来解决这个问题。问题是我不知道如何在Xamarin.UWP应用程序中做到这一点,任何想法都是非常有用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-23 06:43:09

如何更改GestureSettings应用程序的Xamarin.UWP?

从官方的文档派生出来,触摸可以产生一个保持动作,但是鼠标设备通常不能。所以,如果你想要实现鼠标保持,你需要使用添加GestureRecognizer为您的元素,并使用GestureRecognizer的保持事件来实现鼠标保持。有关更多细节,请参考以下内容。

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
    }
}

使用

代码语言:javascript
复制
<StackLayout>
    <Label
        effect:MouseHoldingEffect.MouseHolding="{Binding MouseHoldingAction}"
        FontSize="25"
        Text="Hello" VerticalOptions="Center" HorizontalOptions="Center" Margin="100"/>
</StackLayout>

ViewModel

代码语言:javascript
复制
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;
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64473920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档