Search code examples
c#eventsxamarin.iosevent-handlinguitapgesturerecognizer

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


I am new to deletegate so i dont know how to fix these issue Evenhandler is null and it cannot it main method once clicked here is my code

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

When TapEvent is return null why it happens how to handled PointEventArgs is class to initiate coordinate x and y axis

Renderer ios code

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 code

<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>

PageModel

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

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

                                                           });

ImageTapCommand cannot trigger when I debug and found TapEvent is null. Thanks advance


Solution

  • TapEvent is null because you not subscribed on it. According to MSDN: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap, you can try to subscribe through:

    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>
    

    or 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>
    

    In your example i didn't see Command handler, but you telling that it isn't fired (maybe it not exists?). But there is kind of tap event handler TapHandler(UITapGestureRecognizer tgr), which may be used as Tapped handler.