2017-07-21 28 views
0

我们的系统中有几个BindableProperties。他们大部分都在工作,而我之前没有遇到过这个问题。我正在测试UWP,但其他平台上的问题可能相同。Xamarin Forms - BindableProperty不工作

你可以看到在这里下载代码,看看到底是什么我谈论 https://[email protected]/ChristianFindlay/xamarin-forms-scratch.git

这里是我的代码:

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

这是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> 

我可以看到在Test的setter中,1被传递给SetValue。但是,在下一行中,我查看GetValue作为监视窗口中的属性,并且值为0. BindableProperty不会粘住。我试着用几个不同的Create重载实例化BindingProperty,但似乎没有任何工作。我究竟做错了什么?

回答

0

对于初学者,您正在使用的方法BindableProperty.Create已被弃用,我建议更改它。另外,我认为你应该使用propertyChanged:而不是propertyChanging:例如:

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