gpt4 book ai didi

c# - 完成按钮未触发Xamarin Entry上的Completed事件

转载 作者:行者123 更新时间:2023-12-01 20:03:24 24 4
gpt4 key购买 nike

在Xamarin Forms中添加了Done button on iOS数字键盘之后,我遇到了另一个问题:完成按钮未触发完成事件(如返回按钮)。
在实现此方法的过程中,我在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中解决此问题,因为我认为在那个人发布代码的那一刻,工作了。

谢谢!

最佳答案

实际上为SendCompleted()添加了IEntryController,因此您不再需要为此使用反射。实际上,这种方式似乎不再起作用。只需像这样从按钮按下就直接调用SendCompleted()

    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;
}

关于c# - 完成按钮未触发Xamarin Entry上的Completed事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39529989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com