Xamarin.Forms.Map 显示来自命令的 PIN

Xamarin.Forms.Map show PIns from Command

public partial class ClientMap: ContentPage
   {
ClientCoordinateviewModel viewModel= new ClientCoordinateviewModel ();
       Client client;
       public ClientMap(Client selected_client)
       {
           Title = klient.Select_client_name;
           InitializeComponent();
           BindingContext = viewModel;
           client= selected_client;
            map = new Map(MapSpan.FromCenterAndRadius(new Position(37, -122), Distance.FromMiles(10)));

           //Just a test pin
           var pin = new Pin()
           {
               Position = new Position(37, -122),
               Label = "Some Pin!"
           };
           map.Pins.Add(pin);
           Content = map;


       }
       protected override void OnAppearing()
       {
           base.OnAppearing();
           if (viewModel.CoordinateParnertPins.Count == 0)
           {
               viewModel.CommandGetCoordinatePins.Execute(client);

           }
       }
}

我正在尝试使用 MVVM 模式。当命令执行时,我得到一个引脚列表。我的问题是如何将图钉添加到地图?如果在 ClientMap() 构造函数中定义,则引脚列表为空。有什么建议吗?

在 ViewModel 中

获取引脚列表后调用以下代码

 MessagingCenter.Send<Object, List<Position>>(this, "addPins", PinList); // PinList is type of List<Position>

在内容页面

public ClientMap(Client selected_client)
{
   //...
   map = new Map(MapSpan.FromCenterAndRadius(new Position(37, -122), Distance.FromMiles(10)));

   MessagingCenter.Subscribe<Object, List<Position>>(this, "addPins", (args,list) => {
            
     foreach(Position position in list)
     {
       map.Pins.Add(position);
     }
   });
  Content = map;
}