【问题标题】:xamarin.forms - text binding to EntryRendererxamarin.forms - 文本绑定到 EntryRenderer
【发布时间】: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


    【解决方案1】:

    好的。我不确定这是否可行,但您可以尝试一下:

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

    希望这会有所帮助。

    【讨论】:

    • 我将它添加到代码中,不幸的是它看起来并没有改变任何东西:/.. 只实现你的代码应该影响绑定属性?还是我应该添加其他代码?
    • 你的代码中不应该是“TestValue”吗?
    • 我发布的代码只是覆盖了Entry的Text属性。休息应该表现相同。您可以尝试将属性名称从“文本”更改为其他名称并在 XAML 中使用它。您还可以在 getter/setter 处添加断点以查看其是否执行代码。还要查看 OnElementPropertyChanged 方法。你可能需要实现它。
    • 是的。我使用了调试器,甚至没有执行代码。我猜应该使用 OnElementPropertyChanged 方法来触发该代码?你能告诉我应该在哪里实现它吗?
    • 好的,我再次创建了这个字段并且绑定开始工作(没有你的代码).....失去了时间。
    猜你喜欢
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    相关资源
    最近更新 更多