【发布时间】:2017-01-24 14:15:52
【问题描述】:
在 Xamarin Forms 中添加 Done button on iOS 数字键盘后,我遇到了另一个问题:完成按钮没有触发 Completed 事件(就像返回按钮一样)。 在实现这一点的过程中,我在Xamarin Forums 上找到了以下代码:
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: github.com/xamarin/Xamarin.Forms/blob/…
标签: c# ios xamarin xamarin.ios