[英]Xaml using hidden property
我做了一个自定义控件,它具有属性X
隐藏了父级的VisualElement.X
属性。
public class MyCustomControl : ContentView // is a distant child of VisualElement
{
public new double X
{
get { return 0; }
set { Console.WriteLine("I was not called with: " + value); }
}
}
我在xaml中设置了自定义控件的X
:
<controls:MyCustomControl X="10" />
但是,在此调用了Xamarin.Forms.VisualElement.X
属性设置器,而不是MyCustomControl.X
设置器。 为什么? 以及如何使它改为使用我的自定义控件属性?
作为旁注。 当x:Name="myCustomControl
并且myCustomControl.X = 10
在后面的代码中时-则调用MyCustomControl
的setter。
声明父级中不存在的属性时:
public double AnotherX
{
get { return 0; }
set { Console.WriteLine("Was called with: " + value); }
}
设置员被称为。 (来自xaml)。
这是因为您正在通过Xaml设置VisualElement的BindableProperty'X'。 如果您还在自定义控件中创建BindableProperty'X',它也应该起作用。
您创建一个bindableproperty不仅是一个属性,请参见下面的如何制作一个可绑定属性
private readonly BindableProperty CustomXProperty = BindableProperty.Create(nameof(CustomX), typeof(double), typeof(MyCustomControl), defaultValue: 0.0); public double CustomX { get { return (double)GetValue(CustomXProperty); } set { SetValue(CustomXProperty, value); } }
请参阅此处以获取更多信息https://docs.microsoft.com/zh-cn/xamarin/xamarin-forms/xaml/bindable-properties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.