在Xamarin Forms中添加了Done button on iOS数字键盘后,我遇到了另一个问题:完成按钮未触发Completed事件(如返回按钮)。 在我的方式来实现这一点,我发现Xamarin Forums下面的代码:完成按钮未触发在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);
我试着调试问题,我发现SendCompleted
方法不存在,但我不知道如何解决Xamarin的最新版本这个问题,因为...我认为在那个人发布代码的那一刻,工作。
谢谢!
这看起来像一个完整的黑客给我...无论如何,你可以尝试直接调用'Completed'事件,而不是使用反射。它被宣布为'public':https://github.com/xamarin/Xamarin.Forms/blob/5e553f6195e66e48688b8ab324f1bab1e9251f0a/Xamarin.Forms.Core/Entry.cs#L96 – Krumelur