【发布时间】:2018-02-17 00:33:57
【问题描述】:
我正在制作 xamarin.forms 应用程序。我的项目的一个要求是焦点输入不应该弹出系统键盘。所以我用这个指南做了一个自定义条目:Keyboard disabled guide
一切都很好。但是还有一个问题..
如果我做简单的绑定:
<local:SoftKeyboardDisabledEntry
Placeholder="Keyboard disabled Entry Control..."
x:Name="SoftKeyboardDisabledEntry"
Text="{Binding TestValue}"/>
它没有反应..(我确信 ViewModel 工作正常,因为“正常”条目工作正常)
所以。 问题是,有没有办法绑定到这个字段?
自定义条目代码: 在 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
{
}
}
编辑/解决方案:这段代码可以正常工作,我再次创建了这个条目并且它正在工作,我无法解释什么是坏的。
【问题讨论】:
标签: c# xamarin mvvm binding xamarin.forms