C# 在EditText上实现ITextWatcher会引发错误

C# 在EditText上实现ITextWatcher会引发错误,c#,xamarin,android-edittext,visual-studio-2017,textwatcher,C#,Xamarin,Android Edittext,Visual Studio 2017,Textwatcher,好的,我正在设置一个全包控件,用于接受扫描输入,然后在文本更改时执行一些功能。我已经在我的EditText上实现了ITextWatcher,当我导航到带有该控件的片段时,我会得到一个错误。下面是控件中的必要元素 public class ScannerEditText : EditText, View.IOnKeyListener, View.IOnFocusChangeListener, ITextWatcher { private void Init() {

好的,我正在设置一个全包控件,用于接受扫描输入,然后在文本更改时执行一些功能。我已经在我的EditText上实现了ITextWatcher,当我导航到带有该控件的片段时,我会得到一个错误。下面是控件中的必要元素

public class ScannerEditText : EditText, View.IOnKeyListener, View.IOnFocusChangeListener, ITextWatcher
{
    private void Init()
    {
        this.Focusable = true;
        this.FocusableInTouchMode = true;
        //this.SetOnKeyListener(this);
        this.AddTextChangedListener(this);
        //this.OnFocusChangeListener = this;
    }

    private Timer _refreshTimer = null;
    private string _priorText = null;

    public ScannerEditText(Context context)
        : base(context)
    {
        this.Init();
    }

    public ScannerEditText(Context context, IAttributeSet attrs)
        :base(context, attrs)
    {
        this.Init();
    }

    public ScannerEditText(Context context, IAttributeSet attrs, int defStyleAttr)
        :base(context, attrs, defStyleAttr)
    {
        this.Init();
    }

    public ScannerEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes)
        :base(context, attrs, defStyleAttr, defStyleRes)
    {
        this.Init();
    }

    void ITextWatcher.AfterTextChanged(IEditable s)
    {
        if (this._refreshTimer != null) this._refreshTimer.Dispose();
        this._refreshTimer = new Timer(delegate (object state)
        {
            this._refreshTimer = null;
            OnUpdateData(this, new EventArgs());
        }, false, 500, 0);
    }

    void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after)
    {
        this._priorText = s.ToString();
    }

    void ITextWatcher.OnTextChanged(ICharSequence s, int start, int before, int count)
    {
        string newText = s.ToString();

        //if isEmpty do not advance
        if (string.IsNullOrWhiteSpace(newText))
            return;

        if (newText.Length < this._priorText.Length)
        {
            //if 1 character deleted do not advance
            if (newText == this._priorText.Substring(0, this._priorText.Length - 1))
                return;
        }
        else if (this._priorText.Length < newText.Length)
        {
            //if 1 character added do not advance
            if (newText.Substring(0, newText.Length - 1) == this._priorText)
                return;
        }

        UIUtil.HideKeyboard((Activity)this.Context, (View)this);

        ((View)this.Parent).FindViewById<EditText>(this.NextFocusRightId).RequestFocus();
    }
}
公共类ScannerEditText:EditText,View.IOnKeyListener,View.IOnFocusChangeListener,ITextWatcher
{
私有void Init()
{
这个.Focusable=true;
this.FocusableInTouchMode=true;
//this.SetOnKeyListener(this);
this.AddTextChangedListener(this);
//this.OnFocusChangeListener=此;
}
专用定时器_refreshTimer=null;
私有字符串_priorText=null;
公共扫描编辑文本(上下文)
:基本(上下文)
{
this.Init();
}
公共扫描编辑文本(上下文,IAttributeSet属性)
:base(上下文、属性)
{
this.Init();
}
公共ScannerEditText(上下文上下文、IAttributeSet属性、int-defStyleAttr)
:base(上下文、属性、defStyleAttr)
{
this.Init();
}
公共ScannerEditText(上下文上下文、IAttributeSet属性、int-defStyleAttr、int-defStyleRes)
:base(上下文、属性、defStyleAttr、defStyleRes)
{
this.Init();
}
无效ITextWatcher.PostextChanged(IEditable s)
{
如果(this.\u refreshTimer!=null)this.\u refreshTimer.Dispose();
此._refreshTimer=新计时器(委托(对象状态)
{
这是。_refreshTimer=null;
onUpdate数据(这是新的EventArgs());
},假,500,0);
}
void ITextWatcher.BeforeTextChanged(ICharSequence s,int start,int count,int after)
{
这个。_priorText=s.ToString();
}
void ITextWatcher.OnTextChanged(ICharSequence s,int start,int before,int count)
{
字符串newText=s.ToString();
//如果我没有空,就不要前进
if(string.IsNullOrWhiteSpace(newText))
返回;
if(newText.Length
然后,我导航到该控件上的任何片段,并得到以下错误: System.NotSupportedException:无法从本机句柄激活AndroidMobileBarcodeForMieTrakAPI.Xamarin.Controls.ScannerEditText类型的实例

现在,如果我删除ITextWatcher接口和实现,其他一切都可以正常工作


搜索后我看到的大部分都是java,它在重写成员而不是实现成员时的工作方式略有不同。

您需要使用
IntPtr
JniHandleOwnership
公开构造函数:

protected ScannerEditText(IntPtr javaReference, JniHandleOwnership transfer)
    : base(javaReference, transfer)
{
}