【问题标题】:Xamarin.Forms Gesture Recognizer - The event 'View.GenericMotion' can only appear on the left hand side of += or -=Xamarin.Forms 手势识别器 - 事件“View.GenericMotion”只能出现在 += 或 -= 的左侧
【发布时间】:2019-02-21 17:02:40
【问题描述】:

我一直在研究有关事件以及它们如何需要用于存储添加的回调的支持委托字段的问题的许多答案。但是,我仍然非常困惑这如何适用于我实施它的方式。以下是我根据问题和答案尝试遵循和实施的确切答案,但无济于事。

https://stackoverflow.com/a/41641881/8065149

总的来说,我正在尝试将自定义手势添加到 Xamarin 和 Android 中的自定义地图渲染中。我希望能够在点击并按住时向我的地图添加图钉。我目前已经想出了如何通过水龙头添加引脚,但我想将其更改为长水龙头。下面是我用来尝试向我的项目添加手势的教程,因为它看起来很有希望(即使他的网站不安全)。网站下方还有 github 项目,以防更容易理解。

https://arteksoftware.com/gesture-recognizers-with-xamarin-forms/ https://github.com/RobGibbens/XamarinFormsGestureRecognizers

下面是有问题的代码。具体来说,if (this.GenericMotion != null)if (this.Touch != null) 这两行。这两行都会抛出错误:The event 'View.GenericMotion' can only appear on the left hand side of += or -=

    protected override void OnElementChanged (ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged (e);

        if (e.NewElement == null) {
            if (this.GenericMotion != null) {
                this.GenericMotion -= HandleGenericMotion;
            }
            if (this.Touch != null) {
                this.Touch -= HandleTouch;
            }
        }

        if (e.OldElement == null) {
            this.GenericMotion += HandleGenericMotion;
            this.Touch += HandleTouch;
        }
    }

    void HandleTouch(object sender, TouchEventArgs e)
    {
        _detector.OnTouchEvent(e.Event);
    }

    void HandleGenericMotion(object sender, GenericMotionEventArgs e)
    {
        _detector.OnTouchEvent(e.Event);
    }

我试图按照我之前发布的答案进行操作,但我很困惑我应该如何检查它们是否为空,然后如果它们是我应该如何调用实际使用的处理程序触摸事件。

如果需要更多代码来阐明问题,请发表评论。谢谢。

【问题讨论】:

标签: c# events xamarin.forms xamarin.android event-handling


【解决方案1】:

为了简单起见,即使EventHandlernull,删除处理程序也是安全的。因此:

this.GenericMotion -= HandleGenericMotion;
this.Touch -= HandleTouch;

即使GenericMotion 和/或Touchnull,也可以安全调用。

请注意,在删除处理程序并稍后重新添加时,如果触发了事件,则将不会处理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多