2016-09-16 17 views
1

Done button on iOS数値キーボードをXamarinフォームに追加した後、別の問題が発生しました。完了ボタンが押されなかった完了イベント(戻りボタンのように)。私はなぜ知らないが、私はエラーが表示さ終了ボタンが押されない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; 
    } 
} 
} 
} 

: はこれを実装するために私の方法で、私はXamarin Forumsに次のコードを発見した行で

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の最新のバージョンでは、この問題を解決する方法を理解していないbecaus私はその男がコードを投稿した瞬間には、働いたと思う。

ありがとうございます!

+0

これは完全なハックのようです...とにかく、あなたはリフレクションを使わずに直接「完了」イベントを呼び出すことができます。 'public'と宣言されています:https://github.com/xamarin/Xamarin.Forms/blob/5e553f6195e66e48688b8ab324f1bab1e9251f0a/Xamarin.Forms.Core/Entry.cs#L96 – Krumelur

答えて

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

ありがとうございました!それは素晴らしい仕事! :) – Silviu

関連する問題