C# Xamarin表单-BindableProperty不工作
我们的系统中有几个BindableProperty。它们大部分都有效,我以前从未遇到过这个问题。我正在UWP上测试,但问题可能在其他平台上也是一样 你可以在这里下载这段代码,看看我到底在说什么 这是我的密码:C# Xamarin表单-BindableProperty不工作,c#,binding,uwp,xamarin.forms,C#,Binding,Uwp,Xamarin.forms,我们的系统中有几个BindableProperty。它们大部分都有效,我以前从未遇到过这个问题。我正在UWP上测试,但问题可能在其他平台上也是一样 你可以在这里下载这段代码,看看我到底在说什么 这是我的密码: public class ExtendedEntry : Entry { public static readonly BindableProperty TestProperty = BindableProperty.Create<ExtendedEntry,
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;
}
}
公共类扩展条目:条目
{
公共静态只读BindableProperty TestProperty=
BindableProperty.Create
(
p=>p.检验,
0,
BindingMode.TwoWay,
属性更改:TestChanging
);
公共整数测试
{
得到
{
返回(int)GetValue(TestProperty);
}
设置
{
SetValue(TestProperty,value);
}
}
私有静态void TestChanging(BindableObject bindable、int-oldValue、int-newValue)
{
var ctrl=(ExtendedEntry)可绑定;
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>
我可以看到,在测试的setter中,1被传递到SetValue。但是,在下一行中,我查看了watch窗口中属性的GetValue,以及值0。BindableProperty没有粘住。我尝试过用一些不同的Create重载实例化BindingProperty,但似乎没有任何效果。我做错了什么?对于初学者,您正在使用的
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;
}