我做了一个自定义条目,主要是隐藏键盘上的焦点。一切都很好,但有一个小问题。
当我通过EntryText=""
修改我的vm中的绑定文本时,我的Entry中的文本消失了,但键盘弹出...我在android项目中测试了一个TextChanged监听器,但它没有做出反应当我使用EntryText = ""
我的自定义条目如下所示
[assembly: ExportRenderer(typeof(SoftKeyboardDisabledEntry), typeof(SoftkeyboardDisabledEntryRenderer))]
namespace SkanerDetali.Droid {
class SoftkeyboardDisabledEntryRenderer: EntryRenderer {
protected override void OnElementChanged(ElementChangedEventArgs < Entry > e) {
base.OnElementChanged(e);
if (Control != null) {
Control.Click += (sender, evt) => {
new Handler().Post(delegate {
var imm = (InputMethodManager) Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
Console.WriteLine(result);
Control.RequestFocus();
});
};
Control.FocusChange += (sender, evt) => {
new Handler().Post(delegate {
try {
var imm = (InputMethodManager) Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
Console.WriteLine(result);
Control.RequestFocus();
} catch (Exception Ex) {
Console.Write(Ex);
}
});
};
}
}
private void OnPropertyChanging(object sender, PropertyChangingEventArgs propertyChangingEventArgs) {
// Check if the view is about to get Focus
if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName) {
// incase if the focus was moved from another Entry
// Forcefully dismiss the Keyboard
InputMethodManager imm = (InputMethodManager) this.Context.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);
}
}
}
}
xaml中的条目:
<local:SoftKeyboardDisabledEntry
x:Name="SoftKeyboardDisabledEntry"
Text="{Binding ReadedEAN}"
IsVisible="True"
/>
android项目监听器中的TextChanged没有重新启动:
Control.TextChanged += (object sender, Android.Text.TextChangedEventArgs ev) => {
var imm = (InputMethodManager) Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
Console.WriteLine(result);
};
您知道我该如何解决这个问题?我几个小时就在努力......
编辑解决方案......:
我以更好的形式改变了听众:
Control.TextChanged += (sender, evt) = >{
new Handler().Post(delegate {
var imm = (InputMethodManager) Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
Console.WriteLine(result);
});
};
答案 0 :(得分:0)
您可以尝试在自定义渲染器中覆盖OnElementPropertyChanged
:
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(Entry.Text)
|| e.PropertyName == nameof(Entry.IsFocused))
{
//if there is change in text or focus state - act
// i.e hide or show keyboard?
}
}