【问题标题】:Done button not firing Completed event on Xamarin Entry完成按钮未触发 Xamarin 条目上的已完成事件
【发布时间】: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中解决这个问题,因为我想在那一刻家伙发布了代码,工作。

谢谢!

【问题讨论】:

标签: c# ios xamarin xamarin.ios


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-01-08
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2019-05-18
    相关资源
    最近更新 更多