完成按钮未触发 Xamarin Entry 上的 Completed 事件 [英] Done button not firing Completed event on Xamarin Entry

查看:29
本文介绍了完成按钮未触发 Xamarin Entry 上的 Completed 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我添加了 iOS 上的完成按钮 Xamarin Forms 中的数字键盘,我遇到了另一个问题:完成按钮没有触发 Completed 事件(就像返回按钮一样).在实现这一点的过程中,我在 Xamarin 论坛 上找到了以下代码:

After I've added the Done button on iOS numeric keyboard in Xamarin Forms, I encountered another problem: the Done button not firing Completed event (like return button does). On my way to implement this, I found the following code on 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;
    }
}
}
}

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

I don't know why, but I receive the error:

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中解决这个问题,因为我认为在那个人发布代码的那一刻,工作了.

I’ve tried to debug the problem and I found that the SendCompleted method does not exist, but I don’t understand how to solve this problem in the lastest version of Xamarin, because I think on the moment when that guy posted the code, worked.

谢谢!

推荐答案

SendCompleted() 实际上是为 IEntryController 添加的,所以你不需要为此使用反射不再.事实上,这种方式似乎不再有效.只需像这样直接从您的按钮按下调用 SendCompleted() 即可.

SendCompleted() was actually added for IEntryController so you don't need to use reflection for this any longer. In fact it appears that way no longer works. Just call SendCompleted() directly from your button press like so.

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

这篇关于完成按钮未触发 Xamarin Entry 上的 Completed 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆