我的代码中有自定义条目,但我希望使用xamarin.form.behavior
属性。
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/behaviors/
我的最终目标是,在XAML中键入Behavior = NameValidation
或Behavior = IdentityValidation
,然后它会找到已经从Behavior
类继承的不同验证。
我应该如何探索自定义条目中的隐藏属性并实现它?
答案 0 :(得分:0)
首先声明自定义条目类:
public class YourCustomEntry : Entry
{
public YourCustomEntry()
{
}
public string YourCustomField { get; set; }
// You could also create a BindableProperty: http://xamandor.me/2015/02/27/custom-controls-with-xamarin-forms/
}
然后创建自定义行为:
public class NumericValidationBehavior : Behavior<YourCustomEntry>
{
protected override void OnAttachedTo(YourCustomEntry entry)
{
entry.TextChanged += OnEntryTextChanged;
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(YourCustomEntry entry)
{
entry.TextChanged -= OnEntryTextChanged;
base.OnDetachingFrom(entry);
}
void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
// Do something with you custom property
((YourCustomEntry)sender).YourCustomField = "test";
}
}