好吧,所以我设置了一个全包控制来接受扫描输入,然后在文本被更改时执行一些功能。我在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();
}
}
然后我导航到此控件的任何片段并获得以下错误: System.NotSupportedException:无法从原生句柄激活AndroidMobileBarcodeForMieTrakAPI.Xamarin.Controls.ScannerEditText类型的实例
现在,如果我删除了ITextWatcher接口并且实现了其他所有工作
我在搜索之后所看到的一切主要是java,它有点不同,它会覆盖成员而不是实现它们。
答案 0 :(得分:1)
您需要使用IntPtr
和JniHandleOwnership
公开构造函数:
protected ScannerEditText(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}