C# 装订不';t工作-未找到属性、可绑定属性或事件错误
我在绑定方面遇到了问题,但我首先搜索了几个问题,但运气不好,下面是我得到的错误: 错误:位置18:36。找不到属性、可绑定属性或事件 对于“Lat”,或值和属性之间的类型不匹配 下面是我的xaml文件:C# 装订不';t工作-未找到属性、可绑定属性或事件错误,c#,xamarin.forms,C#,Xamarin.forms,我在绑定方面遇到了问题,但我首先搜索了几个问题,但运气不好,下面是我得到的错误: 错误:位置18:36。找不到属性、可绑定属性或事件 对于“Lat”,或值和属性之间的类型不匹配 下面是我的xaml文件: <controls:MapView x:Name="map" VerticalOptions="FillAndExpand"> <controls:MapView.Center> <controls:Posit
<controls:MapView x:Name="map" VerticalOptions="FillAndExpand">
<controls:MapView.Center>
<controls:Position Lat="{Binding latitude}" Long="{Binding longitude}" />
</controls:MapView.Center>
</controls:MapView>
我缺少什么?问题似乎是
Position
类中缺少可公开访问的可绑定属性(请注意,错误提到Lat
,它是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); }
}
// ...
我建议你看一下这本书。基本上,您收到的错误消息是,当尝试使用访问器
Lat
绑定时,它正在查找LatProperty
。无法使用Lat和Long属性的原因是,如果您检查类,其中没有为它们定义可绑定属性,这意味着您无法在XAML中访问它们
一个可能的解决方案是下载示例项目,获取代码并进行相应的更改,使其具有可绑定的属性
为此,您可以检查@Aaron的答案代码示例中的绑定使用MapView的BindingContext。但是,您正在为DisplayMap设置BindingContext。请尝试改为设置MapView控件的BindingContext…您使用的是哪个贴图控件?你确定Lat和Long是可绑定的属性吗?我正在使用一个名为@Jason的库。让我检查他们的库是否具有可绑定属性。这意味着我无法绑定,除非我将mapView库编辑到此代码。@LutaayaHuzaifahIdris如果在库中他们没有实现
Lat
和Long
作为可绑定属性,那么您可以尝试编辑库中的Position
类,或者,在您的项目中,您可以创建一个从位置继承的新类,并在该新类中实现可绑定属性(如果您可以覆盖Lat
和Long
,那么这可能是最简单的)。实际上,我已经开始编辑它,但是当前上下文中不存在this.GetValue
和this.SetValue
。我还阅读了您提供的文档链接。请确保您的类继承自BindableObject
,这些方法就是在这里定义的。如果您希望避免绑定,类似的操作应该可以。。。XAML:
。代码隐藏(在DisplayMap
constructor中):this.map.Center=newposition{Lat=this.latitude,Long=this.longitude}代码>
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); }
}
// ...