我正在创建一个具有ContentView
字段的控件(源自Frame
):
public class SelectionFrame : ContentView
{
Frame f1;
public SelectionFrame()
{
f1 = new Frame
{
VerticalOptions = LayoutOptions.Center,
BackgroundColor = BorderColor,
HorizontalOptions = LayoutOptions.Center,
Padding = new Thickness(0),
CornerRadius = 7,
HeightRequest = 14,
WidthRequest = 14,
Content = f2
};
Content = f1;
}
我将BackgroundColor
设置为此BindableProperty
:
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(SelectionFrame), defaultValue: Color.Black, defaultBindingMode: BindingMode.TwoWay, propertyChanged: ColorChanged);
public Color BorderColor
{
get { return (Color)GetValue(BorderColorProperty); }
set { SetValue(BorderColorProperty, value); }
}
然后我在另一个自定义控件(RadioButtonsGroup
)中使用此控件,该控件具有FrontColor
BindableProperty
:
public static readonly BindableProperty FrontColorProperty = BindableProperty.Create(nameof(FrontColor), typeof(Color), typeof(RadioButtonsGroup), defaultValue: Color.Black);
public Color FrontColor
{
get { return (Color)GetValue(FrontColorProperty); }
set { SetValue(FrontColorProperty, value); }
}
在这里,我设置BorderColor
的{{1}}:
SelectionFrame
我像这样使用控件:
SelectionFrame circle = new SelectionFrame { ClassId = "r", BorderColor = FrontColor, VerticalOptions = LayoutOptions.Center };
但框架的<local:RadioButtonsGroup FrontColor="Red" x:Name="rbs"/>
未更改,仍然具有默认颜色(黑色)。
答案 0 :(得分:1)
这是因为您没有将BackgroundColor
框架的属性绑定到自定义控件属性。你在构造函数上进行了设置,你不是在聆听变化,在这一行中你正在做什么:
SelectionFrame circle = new SelectionFrame { ClassId = "r", BorderColor = FrontColor, VerticalOptions = LayoutOptions.Center };
要更正它,只需将SelectionFrame构造函数的代码更改为:
public SelectionFrame()
{
f1 = new Frame
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Padding = new Thickness(0),
CornerRadius = 7,
HeightRequest = 14,
WidthRequest = 14,
Content = f2
};
f1.SetBinding(Frame.BackgroundColorProperty, new Binding(nameof(BorderColor)));
f1.BindingContext = this;
Content = f1;
}
现在,您将能够收听此属性的更改。
我希望它有所帮助。