【发布时间】: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