我正在制作xamarin.forms应用程序。我的项目的一个要求是Focus Entry不应弹出系统键盘。所以我使用本指南制作了自定义条目:Keyboard disabled guide
一切都很好。但还有另一个问题..
如果我做简单的绑定:
<local:SoftKeyboardDisabledEntry
Placeholder="Keyboard disabled Entry Control..."
x:Name="SoftKeyboardDisabledEntry"
Text="{Binding TestValue}"/>
它没有做出反应..(我确信ViewModel工作正常,因为&#39;正常&#39;条目工作正常)
因此。 问题,有没有办法绑定到这个字段?
自定义输入代码: 在NewXamarinProject.Droid命名空间
[assembly: ExportRenderer(typeof(SoftKeyboardDisabledEntry), typeof(SoftkeyboardDisabledEntryRenderer))]
namespace NewXamarinProject.Droid
{
public class SoftkeyboardDisabledEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
((SoftKeyboardDisabledEntry)e.NewElement).PropertyChanging += OnPropertyChanging;
}
if (e.OldElement != null)
{
((SoftKeyboardDisabledEntry)e.OldElement).PropertyChanging -= OnPropertyChanging;
}
// Disable the Keyboard on Focus
this.Control.ShowSoftInputOnFocus = false;
}
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);
}
}
}
}
NewXamarinProject命名空间中的:
namespace NewXamarinProject
{
public class SoftKeyboardDisabledEntry : Entry
{
}
}
编辑/解决方案:这段代码工作正常,我再次进入此条目并且它正在工作,我无法解释什么是坏的。
答案 0 :(得分:0)
确定。我不确定这是否有用,但你可以尝试一下:
public class SoftKeyboardDisabledEntry : Entry
{
public static readonly new BindableProperty TextProperty = BindableProperty.Create(propertyName: "Text",
returnType: typeof(string),
declaringType: typeof(SoftKeyboardDisabledEntry),
defaultValue: default(string));
public new string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
希望这有帮助。