我复制/编写了从 Frame
继承的类public class Circle : Frame
{
//private double _radius;
public static readonly BindableProperty RadiusProperty = BindableProperty.Create(nameof(Radius), typeof(double), typeof(Circle), 126.0, BindingMode.TwoWay);
public double Radius
{
get => (double)GetValue(RadiusProperty); //_radius;
set
{
SetValue(RadiusProperty, value);
OnPropertyChanged();
AdjustSize();
}
}
private void AdjustSize()
{
HeightRequest = Radius;
WidthRequest = Radius;
Margin = new Thickness(0,0,0,0);
Padding = new Thickness(0, 0, 0, 0);
CornerRadius = (float) (Radius / 2);
}
public Circle()
{
HorizontalOptions = LayoutOptions.Center;
}
}
消费页面定义了这些BinadableProperties
public static readonly BindableProperty InnerColorProperty = BindableProperty.Create("InnerColor", typeof(Color), typeof(CircleProgressView), defaultValue: Color.FromHex("#34495E"), BindingMode.TwoWay);
public Color InnerColor
{
get => (Color)GetValue(InnerColorProperty);
set => SetValue(InnerColorProperty, value);
}
public static readonly BindableProperty InnerRadiusProperty = BindableProperty.Create("InnerRadius", typeof(double), typeof(CircleProgressView), 126.0, BindingMode.TwoWay);
public double InnerRadius
{
get => (double)GetValue(InnerRadiusProperty);
set => SetValue(InnerRadiusProperty, value);
}
并像这样使用 Circle
<components:Circle Grid.Row="0" BackgroundColor="{Binding InnerColor}" Radius="{Binding InnerRadius}" >
可惜的是,可绑定的设置器以及AdjustSize()都不会被调用,也不会使用默认值。我最后不是矩形,而是矩形。 BackgroundColor 是 Frame 的属性,可以绑定并正常工作。
如果我删除BindableProperty并留下常规的INotify属性
public class Circle : Frame
{
private double _radius;
public double Radius
{
get => _radius;
set
{
_radius = value;
OnPropertyChanged();
AdjustSize();
}
}
private void AdjustSize()
{
HeightRequest = Radius;
WidthRequest = Radius;
Margin = new Thickness(0,0,0,0);
Padding = new Thickness(0, 0, 0, 0);
CornerRadius = (float) (Radius / 2);
}
public Circle()
{
HorizontalOptions = LayoutOptions.Center;
}
}
如果我保持 InnerRadius 绑定
,编译器会抱怨严重性代码描述项目文件行抑制状态 错误位置17:92。未找到“半径”的属性,可绑定属性或事件,或值与属性之间的类型不匹配。 ... \ Components \ CircleProgressView.xaml 17
我可以用硬编码值替换 Radius 绑定,并且该绑定运行良好,会出现一个圆圈。
<components:Circle Grid.Row="0" BackgroundColor="{Binding InnerColor}" Radius="126" >
常规C#类中的BindableProperty有什么问题?
答案 0 :(得分:0)
首先,我们需要在可绑定属性的属性更改事件中处理数据,而不是普通属性的setter方法。因此,像这样修改您的Circle类:
public static readonly BindableProperty RadiusProperty = BindableProperty.Create(nameof(Radius), typeof(double), typeof(Circle), 125.0, BindingMode.TwoWay, propertyChanged: RadiusChanged);
public double Radius
{
get => (double)GetValue(RadiusProperty); //_radius;
set => SetValue(RadiusProperty, value);
}
static void RadiusChanged(BindableObject bindableObject, object oldValue, object newValue)
{
Circle circle = bindableObject as Circle;
circle.HeightRequest = (double)newValue;
circle.WidthRequest = (double)newValue;
circle.CornerRadius = (float)((double)newValue / 2);
}
这是因为我们在XAML中绑定数据,所以我们应该直接操纵可绑定属性的更改事件。
第二,我看到您使用父页面的可绑定属性来绑定属性。通常,我们不会这样做。我们将使用视图模型作为页面的绑定上下文,然后将属性绑定到绑定上下文。但是,如果您确实想将父页面的bindable属性用作Circle的绑定上下文,请尝试以下方式:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sample.SecondPage"
xmlns:components="clr-namespace:Sample"
x:Name="Page">
<ContentPage.Content>
<StackLayout>
<components:Circle BackgroundColor="{Binding InnerColor, Source={x:Reference Page}}" Radius="{Binding InnerRadius, Source={x:Reference Page}}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
首先命名您的父页面,然后将圈子的来源更改为该页面。
在这里,与Radius
相比,我使用了不同的默认InnerRadius
值,因此属性更改事件将在初始时间调用。