2015-08-10 4 views
0

J'ai une entrée personnalisée dans mon code, mais je souhaite utiliser la propriété xamarin.form.behavior.Liaison XAML à une entrée personnalisée dans xamarin.form.behavior

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

Mon objectif final est comme, le type Behavior = NameValidation ou Behavior = IdentityValidation en XAML, puis il ira trouver les différentes validation qui a déjà hérité de Behavior classe.

Comment dois-je explorer la propriété cachée dans l'entrée personnalisée et l'atteindre?

Répondre

0

Tout d'abord vous déclarer la classe d'entrée personnalisée:

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/ 
} 

Ensuite, créez un comportement personnalisé:

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