.net Maui Maps Pin.MarkerClick 事件未触发

问题描述 投票:0回答:1

我的 ViewModel 创建了应映射的引脚列表。在我的页面的 OnAppearing 代码中,我为 MarkerClicked 事件添加代码,如下所示:

protected override void OnAppearing()
{
    try
    {
        base.OnAppearing();

        // Add the pin click events 
        foreach (var pin in _MapPlace.PinsToMap)
        {
            pin.MarkerClicked += async (s, args) =>
            {
                args.HideInfoWindow = true;
                string pinName = ((Pin)s).Label;
                await DisplayAlert("Pin Clicked", $"{pinName} was clicked.", "Ok");
            };
        }

        BindingContext = _MapPlace;
        _MapPlace.MoveMap();
        MapPlaceList.PlaceList = _MapPlace.MasterPlaces;

    }
    catch (Exception ex)
    {
        App.ProcessException(ex);
    }

    _MasterPage.IsBusy = false;
}

当我运行应用程序(在 Android 上)并单击引脚时 - 会显示正常的引脚详细信息(即标题和地址),但 MarkerClick 代码似乎永远不会执行。

这是填充我的虚拟机中的引脚列表的代码。

   private async Task UpdatePinList()
   {
       try
       {
           //Linq Query to get the places to map
           var workPinList = (from p in MasterPlaces
                              where p.Selected == true
                              select new Pin()
                              {
                                  Label = p.Title,
                                  Address = p.Address,
                                  Type = PinType.Place,
                                  Location = new Location(p.Latitude, p.Longitude)
                              }).ToList();
           
           //Add the current location to ensure the area is centered around it
           if (IncludeCurrentLocation)
           {
               var currentLocation = await GeoLocationHelper.GetLocation();
               workPinList.Add(new Pin() { Location = currentLocation });
           }

           //Build the mapspan.
           DisplayArea = GetMapSpan(workPinList);

           //Remove the pin as it will be shown natively via the map
           if (IncludeCurrentLocation)
           {
               workPinList.RemoveAt(workPinList.Count - 1);
           }

           PinsToMap = new ObservableCollection<Pin>(workPinList);

           if (PinsToMap.Count == 1)
           {
               SubTitle = PinsToMap[0].Label;
           }
           else
           {
               SubTitle= string.Concat(AppResources.NumberResultsCaption, " ", PinsToMap.Count.ToString());
           }

           if (WorkingMap != null)
           {
               //Now create and position the map
               MoveMap();
           }
       }
       catch (Exception ex)
       {
           App.ProcessException(ex);
       }
   }

我还确保在 OnAppearing 代码运行之前填充列表,并将 MarkerClick 事件的添加移至列表绑定到地图之前。

关于为什么我似乎 MarkerClick 事件代码没有触发有什么想法吗?

c# maps maui
1个回答
0
投票

所以我相信我已经解决了问题。我能为 MarkerClicked 找到的所有示例都是手动添加到地图中的图钉。我正在使用数据绑定到项目列表。在数据绑定情况下,您需要在 Pin 数据模板的 ItemTemplate 中指定 MarkerClicked 事件处理程序,如下所示(来自我的 XAML):

            <maps:Map x:Name="map" ItemsSource="{Binding PinsToMap}" Margin="0,4,0,4" >
                <maps:Map.ItemTemplate>
                    <DataTemplate x:DataType="{x:Type maps:Pin}">
                        <maps:Pin Location="{Binding Location}"
                          Address="{Binding Address}"
                          Label="{Binding Label}" 
-->This is the line       MarkerClicked="Pin_MarkerClicked"/>
                    </DataTemplate>
                </maps:Map.ItemTemplate>
            </maps:Map>

不幸的是,我无法快速弄清楚如何将其绑定到我的虚拟机的命令,因此我只是让事件背后的代码调用我的虚拟机中的命令。无论如何,现在当单击图钉时,我的 MarkerCLicked 事件就会触发。

我希望这对其他人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.