【发布时间】: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