EventHandler 为空,因此我无法使用 Tapgesturerecognizer 在 MainPage 命令上调用

EventHandler is null so i cant invoke on MainPage Command using Tapgesturerecognizer

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

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

当 TapEvent 为 return null 为什么会发生如何处理 PointEventArgs class 以启动坐标 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无法触发。提前致谢

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 处理程序。