我正在尝试使用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 never gets set to EntryTextType.Any ,并始终使用EntryTextType.Numeric的默认值 . 我在这里错过了什么?谢谢 .
1 回答
我注意到
EntryTypeProperty
声明中存在一些差异 .声明类型应该是
ExtendedEntry
的所有者并且,为了指示XAML使用枚举
TypeConverter
,您必须将数据类型定义为EntryTextType
所以你的新代码看起来像: