简体   繁体   English

Xamarin表单-BindableProperty无法正常工作

[英]Xamarin Forms - BindableProperty Not Working

We have several BindableProperties in our system. 我们的系统中有几个BindableProperty。 They mostly work, and I haven't come across this problem before. 它们大部分都可以工作,而我之前从未遇到过这个问题。 I am testing on UWP, but the problem is probably the same on other platforms. 我正在UWP上进行测试,但问题可能在其他平台上相同。

You can see download this code here to see exactly what I am talking about https://ChristianFindlay@bitbucket.org/ChristianFindlay/xamarin-forms-scratch.git 您可以在此处下载此代码,以准确了解我在说什么https://ChristianFindlay@bitbucket.org/ChristianFindlay/xamarin-forms-scratch.git

Here is my 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;
    }
}

This is the XAML: 这是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 can see that in the setter for Test, 1 is being passed in to SetValue. 我可以看到在Test的setter中,有1个传递给SetValue。 But, on the next line, I look at GetValue for the property in the watch window, and the value 0. The BindableProperty is not sticking. 但是,在下一行,我在监视窗口中查看该属性的GetValue,并将其值设置为0。BindableProperty不粘滞。 I've tried instantiating the BindingProperty with a few different Create overloads, but nothing seems to work. 我尝试使用一些不同的Create重载实例化BindingProperty,但似乎没有任何效果。 What am I doing wrong? 我究竟做错了什么?

For starters the method of BindableProperty.Create you're using has been deprecated and I would suggest changing it. 对于初学者,不建议使用BindableProperty.Create方法。建议您更改它。 Also, I think you should be probably be using propertyChanged: instead of propertyChanging: For example: 另外,我认为您可能应该使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM