【问题标题】:EventHandler is null so i cant invoke on MainPage Command using TapgesturerecognizerEventHandler 为空,因此我无法使用 Tapgesturerecognizer 在 MainPage 命令上调用
【发布时间】:2021-11-27 08:14:32
【问题描述】:

我是 deletegate 的新手,所以我不知道如何解决这些问题 Evenhandler 是 null 并且一旦点击这里它就不能成为主要方法是我的代码

public event EventHandler<CarSchematic.PointEventArgs> TapEvent;
        public void OnTapEvent(float x, float y)
        {
            TapEvent?.Invoke(this, new PointEventArgs(x, y));
        }

当 TapEvent 返回 null 为什么会发生如何处理 PointEventArgs 是类来启动坐标 x 和 y 轴

渲染器ios代码

protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                // Grab the Xamarin.Forms control (not native)
                formsElement = e.NewElement as CustomImage;
                // Grab the native representation of the Xamarin.Forms control
                nativeElement = Control as UIImageView;
                // Set up a tap gesture recognizer on the native control
                nativeElement.UserInteractionEnabled = true;
                UITapGestureRecognizer tgr = new UITapGestureRecognizer(TapHandler);
                nativeElement.AddGestureRecognizer(tgr);
            }
        }

        //
        // Respond to taps.
        //
        public void TapHandler(UITapGestureRecognizer tgr)
        {
            CGPoint touchPoint = tgr.LocationInView(nativeElement);
            formsElement.OnTapEvent(AppState.Xaxis = (float)touchPoint.X, AppState.Yaxis = (float)touchPoint.Y);
        }

Xaml 代码

<local:CustomImage  Source="{Binding DamageModel.PhotoSource}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFit">
                <local:CustomImage.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding BindingContext.ImageTapCommand, Source={x:Reference Damage} }" CommandParameter="{Binding .}" />
                </local:CustomImage.GestureRecognizers>
            </local:CustomImage>

页面模型

public ICommand ImageTapCommand => new FreshCommand(async (obj) =>
                                                           {

                                                               AddDamagePage DamagePage = new AddDamagePage();
                                                               DamagePage.DamageVHCEvent += GoToDamagePage;
                                                               await PopupNavigation.Instance.PushAsync(DamagePage);

                                                           });

当我调试并发现 TapEvent 为空时,无法触发 ImageTapCommand。提前感谢

【问题讨论】:

    标签: c# events xamarin.ios event-handling uitapgesturerecognizer


    【解决方案1】:

    TapEvent 为空,因为您没有订阅它。 根据MSDN:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap,可以尝试通过以下方式订阅:

    Tapped:

    <local:CustomImage Source="{Binding DamageModel.PhotoSource}"
                       HorizontalOptions="FillAndExpand"
                       VerticalOptions="FillAndExpand"
                       Aspect="AspectFit">
        <local:CustomImage.GestureRecognizers>
            <TapGestureRecognizer
                    Tapped="YourTapHandler"  // Here is your tap event handler
                    NumberOfTapsRequired="1" />
      </local:CustomImage.GestureRecognizers>
    </local:CustomImage>
    

    或Command:

    <local:CustomImage Source="{Binding DamageModel.PhotoSource}"
                       HorizontalOptions="FillAndExpand"
                       VerticalOptions="FillAndExpand"
                       Aspect="AspectFit">
        <local:CustomImage.GestureRecognizers>
            <TapGestureRecognizer
                Command="{Binding YourTapCommand}" // Here is your command handler
                CommandParameter="{Binding ...}" />
        </local:CustomImage.GestureRecognizers>
    </local:CustomImage>
    

    在您的示例中,我没有看到 Command 处理程序,但您告诉它没有被触发(也许它不存在?)。但是有一种点击事件处理程序TapHandler(UITapGestureRecognizer tgr),可以用作Tapped处理程序。

    【讨论】:

    • 每当我点击 IOS TapHandler 命中并且 CustomImage 类 Tapevent 为空时仍然无法正常工作,因此它无法调用并且也没有触发命令和 Tapgesture
    • 当我点击页面 TapHandler 触发器并转到 CustomImage 类时,问题是 IOS 我得到了 X 和 Y 协调但 TapEvent 为空,所以我不能调用
    • 您从您的 CustomImage 创建了一个自定义事件。 xaml 中是否有TapEvent 为其设置处理程序或命令?
    • 是的,感谢 Auditive 现在可以正常工作了
    • 我怀疑 TapEvent 只会触发 ImageTapCommand 不会触发任何建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    相关资源
    最近更新 更多