将 XAML 绑定到 xamarin.form.behavior 中的自定义条目

Binding XAML to custom entry in xamarin.form.behavior

我的代码中有一个自定义条目,但我希望使用 xamarin.form.behavior 属性.

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/behaviors/

我的最终目标是,在 XAML 中键入 Behavior = NameValidationBehavior = IdentityValidation,然后它将找到已经从 Behavior [=25] 继承的不同验证=].

我应该如何探索自定义条目中隐藏的 属性 并实现它?

首先声明您的自定义条目class:

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