首页 > 解决方案 > 将引脚添加到 Mapsui(Xamarin 表单)时出现异常

问题描述

我正在尝试向我的 Xamarin Forms Mapsui 控件添加一个引脚。

这是我的 Xaml

<mapsui:MapView x:Name="selectMapControl"
        VerticalOptions="FillAndExpand"
        HorizontalOptions="Fill"
        BackgroundColor="Gray" />

这是我的代码:

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

            var map = new Map
            {
                CRS = "EPSG:3857",
                Transformation = new MinimalTransformation()
            };

            var tileLayer = OpenStreetMap.CreateTileLayer();

            map.Layers.Add(tileLayer);
            map.Widgets.Add(new Mapsui.Widgets.ScaleBar.ScaleBarWidget(map) { TextAlignment = Mapsui.Widgets.Alignment.Center, HorizontalAlignment = Mapsui.Widgets.HorizontalAlignment.Left, VerticalAlignment = Mapsui.Widgets.VerticalAlignment.Bottom });

            selectMapControl.Map = map;

            double lat = 36.9628066;
            double lng = -122.0194722;

            var myPosition = new Position(lat, lng);
            var myPin = new Pin(selectMapControl) //Exception here
            {
               Position = myPosition,
               Type = PinType.Pin,
               Label = "Zero point",
               Address = "Zero point",
            };
            selectMapControl.Pins.Add(myPin);
}

我在var myPin = new Pin(selectMapControl)...行收到“对象引用未设置为对象的实例。”异常

如果我的 lat 和 lng 都设置为 0 那么它可以工作 这是异常中的堆栈跟踪:

   at Mapsui.UI.Forms.Pin.OnPropertyChanged (System.String propertyName) [0x001df] in <5cc65edf513349cdba641e049bbf0143>:0 

在 Xamarin.Forms.BindableObject.SetValueActual(Xamarin.Forms.BindableProperty 属性,Xamarin.Forms.BindableObject+BindablePropertyContext 上下文,System.Object 值,System.Boolean currentApplying,Xamarin.Forms.Internals.SetValueFlags 属性,System.Boolean 无声)[ 0x00114] 在 D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:510 在 Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty 属性,System.Object 值,Xamarin.Forms.Internals。 SetValueFlags 属性,Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] 在 D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446 在 Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty属性,System.Object 值,System.Boolean fromStyle,System.Boolean checkAccess) [0x0004d] 在 D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374 在 Xamarin.Forms.BindableObject.SetValue(Xamarin.Forms.BindableProperty 属性,System.Object 值)[0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349 在 Mapsui.UI .Forms.Pin.set_Position (Mapsui.UI.Forms.Position value) [0x00000] in <5cc65edf513349cdba641e049bbf0143>:0 at eLunaApp.Views.RestaurantMapPage.OnAppearing () [0x0008d] in C:\x\elunaapp\eLunaApp\PageRestaurantMap\ RestaurantMapPage.xaml.cs:82 在 Xamarin.Forms.Page.SendAppearing () [0x00045] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:452 在 Xamarin.Forms.ShellContent.SendPageAppearing ( Xamarin.Forms.Page 页面)[0x0003b] 在 D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:127 在 Xamarin.Forms.ShellContent.SendAppearing () [0x00019] 在 D:\ a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:108 在 Xamarin.Forms.ShellContent.OnChildAdded (Xamarin.Forms.元素子)[0x00021] 在 D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:138 在 Xamarin.Forms.ShellContent.set_ContentCache (Xamarin.Forms.Page 值)[0x0003b] 在 D :\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:182 在 Xamarin.Forms.ShellContent.Xamarin.Forms.IShellContentController.GetOrCreateContent () [0x0003c] 在 D:\a\1\s\ Xamarin.Forms.Core\Shell\ShellContent.cs:68 在 Xamarin.Forms.Platform.Android.ShellSectionRenderer.OnCreateView(Android.Views.LayoutInflater 充气器,Android.Views.ViewGroup 容器,Android.OS.Bundle savedInstanceState)[0x000f2]在 D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellSectionRenderer.cs:166 在 AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater ,System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00021] 在 D:\a\1\s\generated\androidx.fragment.fragment\obj\Release\monoandroid90\generated\src\AndroidX.Fragment.App.Fragment.cs :1950 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.50(intptr,intptr,intptr,intptr,intptr)

标签: xamarin.formsmapsui

解决方案


推荐阅读