[英]Binding doesn't work - No property, bindable property, or event found error
Am having an issue with binding, but I first searched with several questions on this, but no luck, below is the error am getting :我遇到了绑定问题,但我首先搜索了几个关于此的问题,但没有运气,下面是错误:
Error: Position 18:36.
错误:位置 18:36。 No property, bindable property, or event found for 'Lat', or mismatching type between value and property
找不到“纬度”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配
Below is my xaml file :下面是我的xaml文件:
<controls:MapView x:Name="map" VerticalOptions="FillAndExpand">
<controls:MapView.Center>
<controls:Position Lat="{Binding latitude}" Long="{Binding longitude}" />
</controls:MapView.Center>
</controls:MapView>
Then the c#
code is as below :然后
c#
代码如下:
public partial class DisplayMap : ContentPage
{
private double latitude { get; }
private double longitude { get; }
public DisplayMap()
{
InitializeComponent();
this.latitude = 0.3476;
this.longitude = 32.5825;
BindingContext = this;
}
What am I missing ?我错过了什么?
The issue seems to be a lack of publicly-accessible bindable properties in the Position
class (notice that the error mentions Lat
which is a member of Position
).问题似乎是
Position
类中缺少可公开访问的可绑定属性(请注意,错误提到Lat
是Position
的成员)。 Position
should look something like this: Position
应如下所示:
public class Position : BindableObject
{
public static readonly BindableProperty LatProperty = BindableProperty.Create(nameof(Lat), typeof(double), typeof(Position), 0);
public double Lat
{
get { return (double)this.GetValue(LatProperty); }
set { this.SetValue(LatProperty, value); }
}
public static readonly BindableProperty LongProperty = BindableProperty.Create(nameof(Long), typeof(double), typeof(Position), 0);
public double Long
{
get { return (double)this.GetValue(LongProperty); }
set { this.SetValue(LongProperty, value); }
}
// ...
I suggest you take a look at the official documentation for Bindable Properties .我建议您查看Bindable Properties的官方文档。 Essentially, the error message you are getting is because it is looking for
LatProperty
when trying to bind using the accessor Lat
.本质上,您收到的错误消息是因为它在尝试使用访问器
Lat
进行绑定时正在寻找LatProperty
。
The reason why you are not able to use the Lat and Long property is that if you check the Position class there is no Bindable Property defined for them in it which means you cannot access them in XAML,您无法使用 Lat 和 Long 属性的原因是,如果您检查Position类,则其中没有为它们定义的 Bindable 属性,这意味着您无法在 XAML 中访问它们,
A possible solution is to download the Sample project and take the code and make the respective changes for it to have Bindable Properties.一种可能的解决方案是下载示例项目并获取代码并对其进行相应更改以使其具有可绑定属性。
For that, you can check @Aaron's answer为此,您可以查看@Aaron 的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.