【问题标题】:Binding XAML to custom entry in xamarin.form.behavior将 XAML 绑定到 xamarin.form.behavior 中的自定义条目
【发布时间】:2015-08-10 03:12:49
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms behavior


    【解决方案1】:

    首先声明你自定义的入口类:

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

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 2017-01-23
      • 2010-12-16
      • 2017-11-18
      • 2014-12-18
      • 2023-03-03
      • 2016-10-16
      • 1970-01-01
      相关资源
      最近更新 更多