2017-09-08 59 views
0

我在制作xamarin.forms应用程序。我的项目的一个要求是,焦点输入不应该弹出系统键盘。所以我做了一个自定义条目与本指南:Keyboard disabled guidexamarin.forms - 文本绑定到EntryRenderer

一切工作非常好。但是总会有另外一个问题..

如果我做简单的绑定:

<local:SoftKeyboardDisabledEntry 
      Placeholder="Keyboard disabled Entry Control..." 
      x:Name="SoftKeyboardDisabledEntry" 
      Text="{Binding TestValue}"/> 

但这并没有反应。(我相信,视图模型是工作得很好,因为“正常”的条目工作正常)

所以。 问题是,有没有办法使这个领域的绑定?在NewXamarinProject命名空间 在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); 
      } 
     } 
    } 
} 


自定义输入的代码

namespace NewXamarinProject 
{ 
    public class SoftKeyboardDisabledEntry : Entry 
    { 
    } 
} 

编辑/解决方案:此代码工作正常,我做了这个条目再次和它的工作,我不能解释什么是坏的。

回答

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); } 
    } 
} 

希望这会有所帮助。

+0

我将此代码添加到代码中,不幸的是,它看起来并不会改变任何内容:/ ..仅实现您的代码应该影响绑定属性?还是应该添加其他代码? –

+0

并且不应该在你的代码中'TestValue'? –

+0

我发布的代码只是覆盖Entry的Text属性。休息应该表现得一样。您可以尝试将属性名称从“文本”更改为其他名称,并在XAML中使用该名称。你也可以在getter/setter中添加一个断点来查看它是否执行了代码。也看看OnElementPropertyChanged方法。你可能需要实现它。 –