【问题标题】:Google Maps Bindings Xamarin.Forms - Pins not showing on map when using BindingPinsBehaviorGoogle Maps Bindings Xamarin.Forms - 使用 BindingPinsBehavior 时图钉未显示在地图上
【发布时间】:2021-03-18 08:27:00
【问题描述】:

起亚或 StackOverFlow,

我正在创建一个具有大量绑定的 Google 地图页面(我使用了 Google 地图绑定)。这种绑定的一个示例是 pin 绑定 - 如下所示:

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps" 
             xmlns:bindings="clr-namespace:Xamarin.Forms.GoogleMaps.Bindings;assembly=Xamarin.Forms.GoogleMaps.Bindings"
             x:Class="standard.MainPage">

    <StackLayout>
        <maps:Map VerticalOptions="FillAndExpand"
                  MyLocationEnabled="True">
            <maps:Map.Behaviors>
                <bindings:BindingPinsBehavior Value="{Binding Pins}" />
            </maps:Map.Behaviors>
        </maps:Map>
    </StackLayout>

</ContentPage>

视图模型:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.GoogleMaps;
using Xamarin.Forms.GoogleMaps.Bindings;

namespace standard.ViewModels
{
    public class MainPageViewModel : BaseViewModel
    {

       private ObservableCollection<Pin> _pins;
        public ObservableCollection<Pin> Pins
        {
            get => _pins;
            set
            {
                _pins = value;
                RaisePropertyChanged();
            }
        } 

        public MainPageViewModel()
        {
            Pins = new ObservableCollection<Pin>();

            Pins.Add(new Pin()
            {
                Type = PinType.Place,
                Position = new Position(78,77)
            });
                
        }
    }
}

Xaml.CS:

using standard.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.GoogleMaps;

namespace standard
{
    public partial class MainPage : ContentPage
    {
        public MainPageViewModel MainPageViewModel;
        public MainPage()
        {
            InitializeComponent();

            this.BindingContext = MainPageViewModel = new MainPageViewModel();
        }
    }

现在 - 问题是 - 我已按预期设置了 BindingContext,但在地图上看不到图钉。

重要的是我可以看到地图上的图钉,因为我正在制作一个应用程序,用户需要在其中查看活动/派对的位置。我还想要一个 MVVM 方法的解决方案。

(使用安卓棒棒糖)

它应该在地图上的位置

图片

这是我的问题。

谢谢,

起亚派到拉

祝你有美好的一天

编辑:感谢 Leo Zhu,他解决了我的问题 - 我现在正在寻找基于 MVVM 的解决方案,但还是谢谢你

Things I've tried Result
Tried to set the Binding of the pins using the default maps item template. Still did not work or show on the map
Tried to call OnPropertyChanged after adding an item to the observable collection Still the pin did not show on the map
Tried using a Pin instead of an ObservableCollections of Pins Still the Pin was nowhere to be seen on the map
Tried setting the BindingContext of the map itself to the MainPageViewModel Still the Pin is nowhere to be seen
Tried to use an earlier update of the Google Maps Binding NuGet package Still - you guessed it - I cannot see the pin

【问题讨论】:

  • 你知道 78,77 在地图上的位置吗?提示 - 它不在您的屏幕截图中的任何位置
  • 是的@Jason 我已经搜索了整个地图,但它不存在。您还想添加其他内容吗?
  • 添加一个pin后RaisePropertyChanged(nameof(Pins));呢?
  • 我明天试试——谢谢@Shaw(不需要nameof,因为我使用的是'CallerMemberName'属性)
  • 或者在this queation中添加CollectionChaged事件。无论如何,将项目添加到集合不会引起集合的 propertychanged。

标签: c# xaml xamarin xamarin.forms maps


【解决方案1】:

请不要初始化 ObservableCollection Pins。

MainPageViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.GoogleMaps;
using Xamarin.Forms.GoogleMaps.Bindings;

namespace standard.ViewModels
{
    public class MainPageViewModel : BaseViewModel
    {

       public ObservableCollection<Pin> Pins { get; set; }

        public MainPageViewModel()
        {
            
        }

        internal void AddPins()
        {
            Pins?.Add(new Pin()
            {
                Label = $"Pin1",
                Position = new Position(78, 77)
            });
        }
    }
}

Xaml.CS:

using standard.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.GoogleMaps;

namespace standard
{
    public partial class MainPage : ContentPage
    {
        public MainPageViewModel MainPageViewModel;
        public MainPage()
        {
            MainPageViewModel = new MainPageViewModel();
            this.BindingContext = MainPageViewModel;
            InitializeComponent();
            MainPageViewModel.AddPins();
            
        }
    }
}

【讨论】:

  • 非常感谢@jai 提供的解决方案——效果很好! (我可以在 5 小时内授予声誉)
【解决方案2】:

我遇到了同样的问题,一种解决方法是不在viewModel的构造函数中添加Pin,而是在page.xaml.cs中添加,如下:

<maps:Map VerticalOptions="FillAndExpand" x:Name = "myMap"
              MyLocationEnabled="True">
  <maps:Map.Behaviors>
     <bindings:BindingPinsBehavior Value="{Binding Pins}" />
  </maps:Map.Behaviors>
</maps:Map>

public MapPage()
    {
        InitializeComponent();
        myMap.Pins.Add(new Pin
        {
            Label = $"Pin1",
            Position = new Position(78, 77)
        });
    }

【讨论】:

  • 是的,但这会破坏我正在制作的应用程序所需的 MVVM 模式。
  • 如果你找不到更好的方法来使用 MVVM,你可以先使用这个解决方法。
  • 好的,谢谢先生的回答-如果在赏金之后我没有得到解决方案,那么您将成为选择的答案。
  • 如果您找到解决方案,请告诉我。
猜你喜欢
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
相关资源
最近更新 更多