2017-07-21 9 views
0

Wir haben mehrere BindableProperties in unserem System. Sie arbeiten meistens, und ich habe dieses Problem vorher nicht bemerkt. Ich teste auf UWP, aber das Problem ist wahrscheinlich das gleiche auf anderen Plattformen.Xamarin Forms - BindableProperty funktioniert nicht

Sie können diesen Code sehen hier herunterladen, um zu sehen genau das, was ich rede https://[email protected]/ChristianFindlay/xamarin-forms-scratch.git

Hier ist mein Code:

public class ExtendedEntry : Entry 
{ 
    public static readonly BindableProperty TestProperty = 
     BindableProperty.Create<ExtendedEntry, int> 
     (
     p => p.Test, 
     0, 
     BindingMode.TwoWay, 
     propertyChanging: TestChanging 
    ); 

    public int Test 
    { 
     get 
     { 
      return (int)GetValue(TestProperty); 
     } 
     set 
     { 
      SetValue(TestProperty, value); 
     } 
    } 

    private static void TestChanging(BindableObject bindable, int oldValue, int newValue) 
    { 
     var ctrl = (ExtendedEntry)bindable; 
     ctrl.Test = newValue; 
    } 
} 

Dies ist die XAML:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:local="clr-namespace:TestXamarinForms" 
      x:Class="TestXamarinForms.BindablePropertyPage"> 
    <ContentPage.Content> 
     <StackLayout> 
      <local:ExtendedEntry Test="1" /> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

I kann sehen, dass im Set von Test 1 an SetValue übergeben wird. Aber in der nächsten Zeile betrachte ich GetValue für die Eigenschaft im Watch-Fenster und den Wert 0. Die BindableProperty klebt nicht. Ich habe versucht, die BindingProperty mit ein paar verschiedenen Create-Überladungen zu instanziieren, aber nichts scheint zu funktionieren. Was mache ich falsch?

Antwort

0

Für den Anfang ist die Methode BindableProperty.Create, die Sie verwenden, veraltet, und ich würde vorschlagen, sie zu ändern. Außerdem denke ich, dass Sie wahrscheinlich propertyChanged: anstelle von propertyChanging: verwenden sollten. Zum Beispiel:

public static readonly BindableProperty TestProperty = BindableProperty.Create(nameof(Test), typeof(int), typeof(ExtendedEntry), 0, BindingMode.TwoWay, propertyChanged: TestChanging); 

public int Test 
{ 
    get { return (int)GetValue(TestProperty); } 
    set { SetValue(TestProperty, value); } 
} 

private static void TestChanging(BindableObject bindable, object oldValue, object newValue) 
{ 
    var ctrl = (ExtendedEntry)bindable; 
    ctrl.Test = (int)newValue; 
} 
Verwandte Themen