【问题标题】:Custom property of a custom control not being set - xamarin forms未设置自定义控件的自定义属性 - xamarin 表单
【发布时间】:2019-01-17 02:12:18
【问题描述】:

我正在尝试使用 EntryType 属性创建一个自定义条目控件,然后我在自定义渲染器中使用它来设置特定于平台的值。但是从 Xaml 中使用时从不设置 EntryType。这是我的代码:

public class ExtendedEntry : Xamarin.Forms.Entry
{

    public static readonly BindableProperty EntryTypeProperty = BindableProperty.Create(
        propertyName: "EntryType",
        returnType: typeof(int),
        declaringType: typeof(EntryTextType),
        defaultValue: 1
        );

    public EntryTextType EntryType
    {
        get
        {
            return (EntryTextType)GetValue(EntryTypeProperty);
        }
        set
        {
            SetValue(EntryTypeProperty, value);

        }
    }

    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);

        if (propertyName == EntryTypeProperty.PropertyName)
        {

        }
    }
}

public enum EntryTextType
{
    Any,
    Numeric,
    Url,
    Email
}

public class ExtendedEntryRenderer : EntryRenderer
{
    public ExtendedEntryRenderer(Android.Content.Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            var element = (ExtendedEntry)Element;
            Control.Hint = element.Placeholder;
            switch(element.EntryType)
            {
                case EntryTextType.Numeric:
                    Control.SetRawInputType(Android.Text.InputTypes.ClassNumber);
                    break;
                default:
                    break;
            }
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var p = e.PropertyName;
        base.OnElementPropertyChanged(sender, e);
    }

}

然后在 XAML 中,我像这样使用控件:

<controls:ExtendedEntry Placeholder="Password" IsPassword="True" Text="{Binding Secret}" EntryType="Any"/>

问题是,EntryType 永远不会设置为 EntryTextType.Any,并且始终使用默认值 EntryTextType.Numeric。我在这里想念什么?谢谢。

【问题讨论】:

    标签: data-binding xamarin.forms custom-controls inotifypropertychanged custom-renderer


    【解决方案1】:

    我注意到EntryTypeProperty 声明中存在一些差异。

    • 声明类型应该是所有者ExtendedEntry
    • 并且,要指示 XAML 使用枚举 TypeConverter,您必须将数据类型定义为 EntryTextType

    所以你的新代码看起来像:

    public static readonly BindableProperty EntryTypeProperty = BindableProperty.Create(
        propertyName: "EntryType",
        returnType: typeof(EntryTextType),
        declaringType: typeof(ExtendedEntry),
        defaultValue: EntryTextType.Numeric
        );
    

    【讨论】:

    • 即使使用整数值 1 也会设置默认值。问题是,xaml 中分配的值永远不会设置。因此,即使使用上述 xaml,EntryType 也始终是 Numeric。
    • 我刚刚测试了新属性 - 它被设置为Any(所以我无法重现您的问题)
    • 但是当我将 returnType 改回 int 时,该值不是通过 XAML 设置的。正如我在答案中提到的,您必须指示 XAML 处理器将 TypeConverter 用于枚举 - 最简单的方法是将 returnType 设置为正确的类型。
    • 哦……给我一分钟验证一下。我不敢相信我连一次都没有尝试将其设置为正确的类型。
    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多