如何在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?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)