EventHandler是空的,所以它不能在主页命令上调用Tapgesturerecognizer



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

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>

PageModel

public ICommand ImageTapCommand => new FreshCommand(async (obj) =>
{
AddDamagePage DamagePage = new AddDamagePage();
DamagePage.DamageVHCEvent += GoToDamagePage;
await PopupNavigation.Instance.PushAsync(DamagePage);
});

ImageTapCommand不能触发当我调试和发现TapEvent是空的。谢谢提前

TapEvent为空,因为您没有订阅它。根据MSDN: https://learn.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处理程序,但你告诉它没有被解雇(也许它不存在?)。但是有一种tap事件处理程序TapHandler(UITapGestureRecognizer tgr),它可以用作Tapped处理程序。

相关内容

  • 没有找到相关文章

最新更新