完成button不被触发在Xamarin Entry上完成的事件

在Xamarin Forms的iOS数字键盘上添加完成button之后,我遇到了另一个问题:完成button没有激发Completed事件(就像返回button一样)。 在我实现这个的方式,我发现Xamarin论坛上的以下代码:

using System; using System.Drawing; using System.Reflection; using Xamarin.Forms.Platform.iOS; using Xamarin.Forms; using UIKit; using KeyboardTest.iOS; using KeyboardTest; [assembly: ExportRenderer(typeof(EntryDone), typeof(EntryDoneRenderer))] namespace KeyboardTest.iOS { public class EntryDoneRenderer : EntryRenderer { // used to cache the MethodInfo so we only have the reflection hit once private MethodInfo baseEntrySendCompleted = null; public EntryDoneRenderer () { } protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged (e); if (this.Element.Keyboard == Keyboard.Numeric) { // only want the Done on the numeric keyboard type UIToolbar toolbar = new UIToolbar (new RectangleF (0.0f, 0.0f, (float)Control.Frame.Size.Width, 44.0f)); var doneButton = new UIBarButtonItem (UIBarButtonSystemItem.Done, delegate { this.Control.ResignFirstResponder (); Type baseEntry = this.Element.GetType(); if(baseEntrySendCompleted==null) { // use reflection to find our method baseEntrySendCompleted = baseEntry.GetMethod("SendCompleted",BindingFlags.NonPublic|BindingFlags.Instance); } try { baseEntrySendCompleted.Invoke(this.Element,null); } catch (Exception ex) { // handle the invoke error condition } }); toolbar.Items = new UIBarButtonItem[] { new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace), doneButton }; this.Control.InputAccessoryView = toolbar; } } } } 

我不知道为什么,但我收到错误:

 System.NullReferenceException: Object reference not set to an instance of an object at myGame.iOS.DoneEntryRenderer.<OnElementChanged>m__0 (System.Object , System.EventArgs ) [0x0005d] in /Users/silviu/Projects/myGame/iOS/DoneEntry.cs:37 

在那一行我有代码: baseEntrySendCompleted.Invoke(this.Element, null);

我试图debugging问题,我发现SendCompleted方法不存在,但我不明白如何解决最新版本的Xamarin中的这个问题,因为我认为在那个人发布代码的那一刻,工作。

谢谢!

SendCompleted()实际上是为IEntryController添加的,所以你不需要再使用reflection。 事实上,这似乎不再有效。 只需调用SendCompleted()直接从你的button按这样的。

  protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); var toolbar = new UIToolbar(new CGRect(0.0f, 0.0f, Control.Frame.Size.Width, 44.0f)); toolbar.Items = new[] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate { Control.ResignFirstResponder(); ((IEntryController)Element).SendCompleted(); }) }; this.Control.InputAccessoryView = toolbar; }