xamarin.forms - 如何在 Xamarin.Forms 中正确绑定视图模型中的地图/引脚?
问题描述
我跟着doc,试图绑定图钉,但失败了。地图始终显示默认位置罗马。这是源代码:
在 DetailPage.xmal 中:
<Frame Margin="10,5"
CornerRadius="10"
Padding="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="300" />
</Grid.RowDefinitions>
<maps:Map MapType="Street" Grid.Row="0" ItemsSource="{Binding WorkPlace}">
<maps:Map.ItemTemplate>
<DataTemplate>
<maps:Pin Position="{Binding Position}"
Address="{Binding Address}"
Label="{Binding Description}" />
</DataTemplate>
</maps:Map.ItemTemplate>
</maps:Map>
</Grid>
</Frame>
在 DetailPageModel.cs 中:
public class DetailPageModel : PageModelBase
{
private Timesheet _detailedTimesheet;
private ObservableCollection<Location> _workPlace;
public ObservableCollection<Location> WorkPlace
{
get => _workPlace;
set => SetProperty(ref _workPlace, value);
}
public ReportDetailPageModel()
{
}
public override async Task InitializeAsync(object navigationData)
{
if (navigationData is Timesheet selectedTimesheet)
{
_detailedTimesheet = selectedTimesheet;
WorkPlace = new ObservableCollection<Location>()
{
new Location(
_detailedTimesheet.ProjectAddress,
"Test Location",
new Position(_detailedTimesheet.ProjectLatitude, _detailedTimesheet.ProjectLongitude))
};
}
await base.InitializeAsync(navigationData);
}
}
在 Location.cs 中:
public class Location : ExtendedBindableObject
{
Position _position;
public string Address { get; }
public string Description { get; }
public Position Position
{
get => _position;
set => SetProperty(ref _position, value);
}
public Location(string address, string description, Position position)
{
Address = address;
Description = description;
Position = position;
}
}
在 ExtendedBindableObject.cs 中:
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
return false;
}
storage = value;
OnPropertyChanged(propertyName);
return true;
}
由于在视图模型中正确接收了 navigationData 并且页面的绑定上下文也在工作,我只是不知道可能缺少什么。任何提示将不胜感激!
实际上我还有一个困惑,为什么官方文档使用自定义 Location 类而不是 Pin 类,因为 Pin 继承自 Element/BindableObject/Object?
解决方案
推荐阅读
- javascript - 计算年龄并检查值是否正常
- python-3.x - 谷歌云功能:达到 --max-instances 时的行为
- google-cloud-platform - gsutil 和 gcloud 有什么区别?
- php - 如何在 TWIG 中获取 Cookie 值?
- elasticsearch - ElasticsearchException:运行机器学习本机代码失败
- user-interface - 统一以不同纵横比为 UI 设置动画的问题
- javascript - 使用 rss-feed 逐项自动滚动网页
- javascript - 尝试将 BASH 安装到 VSCode 时无法编辑用户设置。"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
- mysql - 在我尝试安装 php-mysql 后,DBeaver 6.2.1 停止在我的 Ubuntu 19.04 上运行
- reactjs - 如何在 react redux 中分离一个数组,以便我可以在两列中显示数据