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

时间:2015-08-10 03:12:49

标签: c# xaml xamarin xamarin.forms behavior

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

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

我的最终目标是,在XAML中键入Behavior = NameValidationBehavior = IdentityValidation,然后它会找到已经从Behavior类继承的不同验证。

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

1 个答案:

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