【发布时间】: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