Xamarin.Forms:Image.GestureRecognizers在iOS

时间:2018-01-28 00:02:07

标签: ios xamarin.forms

我使用的是Xamarin.Forms的最新版本。我的页面有ListView,其中显示ViewCells包含Image。现在我想对这些图像的点击做出反应。因此,我添加了此代码

<Image Grid.Column="1" Source="download_cloud.png" WidthRequest="32" HeightRequest="32">
    <Image.GestureRecognizers>
           <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="SynchronizeItem_Clicked"/>
    </Image.GestureRecognizers>
</Image>

这在Android上完美无缺。遗憾的是,这不会对iOS起作用,但会导致异常(Object reference not set to an instance)。奇怪的是,当我将TapGestureRecognizer添加到Label旁边显示的Image时,一切都运行正常。 那么,这是Xamarin.Forms iOS上的错误还是我做错了什么?

编辑:这是堆栈跟踪:

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.Platform.iOS.EventTracker.LoadRecognizers () [0x0005d] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\EventTracker.cs:373 
  at Xamarin.Forms.Platform.iOS.EventTracker.OnElementChanged (System.Object sender, Xamarin.Forms.Platform.iOS.VisualElementChangedEventArgs e) [0x0004e] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\EventTracker.cs:439 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00020] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:279 
  at Xamarin.Forms.Platform.iOS.ViewRenderer`2[TView,TNativeView].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00000] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\ViewRenderer.cs:85 
  at Xamarin.Forms.Platform.iOS
.ImageRenderer.<>n__0 (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) <0x13780f3f0 + 0x0005a> in <13d193f5a21b4e309afc67e0a5508db5>:0 
  at Xamarin.Forms.Platform.iOS.ImageRenderer+<OnElementChanged>d__2.MoveNext () [0x000d6] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\Renderers\ImageRenderer.cs:70 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
  at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users
/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/Foundation/NSAction.cs:163 
--- End of stack trace from previous location where exception was thrown ---
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at SMSIP2.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\Roland\Source\Repos\SMSIP2\SMSIP2\SMSIP2.iOS\Main.cs:12 
2018-01-28 10:06:25.613 SMSIP2.iOS[73968:6382644] Unhandled managed exception:
Object reference not set to an instance of an object (System.NullReferenceException)
  at Xamarin.Forms.Platform.iOS.EventTracker.LoadRecognizers () [0x0005d] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\EventTracker.cs:373 
  at Xamarin.Forms.Platform.iOS.EventTracker.OnElementChanged (System.Object sender, Xamarin.Forms.Platform.iOS.VisualElementChangedEventArgs e) [0x0004e] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\EventTracker.cs:439 
  at Xamarin.Forms.Platform.iOS.VisualElementRenderer`1[TElement].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00020] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\VisualElementRenderer.cs:279 
  at Xamarin.Forms.Platform.iOS.ViewRenderer`2[TView,TNativeView].OnElementChanged (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) [0x00000] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\ViewRenderer.cs:85 
  at Xamarin.Forms.Plat
form.iOS.ImageRenderer.<>n__0 (Xamarin.Forms.Platform.iOS.ElementChangedEventArgs`1[TElement] e) <0x13780f3f0 + 0x0005a> in <13d193f5a21b4e309afc67e0a5508db5>:0 
  at Xamarin.Forms.Platform.iOS.ImageRenderer+<OnElementChanged>d__2.MoveNext () [0x000d6] in D:\agent\_work\1\s\Xamarin.Forms.Platform.iOS\Renderers\ImageRenderer.cs:70 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
  at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] i
n /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/Foundation/NSAction.cs:163 
--- End of stack trace from previous location where exception was thrown ---
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5665/6857dfcc/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at SMSIP2.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\Roland\Source\Repos\SMSIP2\SMSIP2\SMSIP2.iOS\Main.cs:12 
2018-01-28 10

这是回调:

void SynchronizeItem_Clicked(object sender, EventArgs ee)
{

} 

Edit2 :当我在其他地方而不是ListView中使用此代码时,它也在使用iOS

Edit3 :我现在解决了这个问题 - 但解决方案很奇怪,我根本不知道,为什么它正在运行。但是我们走了:我的CellView包含Grid,其中我想放置Image。这不起作用。相反的工作原因是:将Image放在StackLayout内,并将此StackLayout放在Grid中。这看起来就像没有附加StackLayout的代码,突然GestureRecognizer起作用。有人能告诉我为什么会这样吗?

Edit4 :显然这不是一个很好的解决方案,因为Command的{​​{1}}现在也会在您点击GestureListener时执行而不是ViewCell。所以这不是真正的解决方案。

0 个答案:

没有答案