如何修复转换后无法将字符串转换为双精度错误



我对Xamarin很陌生,有点困惑,但我在应用代码时遇到了这个错误:

can't convert string to double error after converting it.

我的代码是:DATA.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace Orbage
{
public class DATA
{
public string Label = "USA";
public string Address = "This is the US";
public string Lat = "40.060407";
public string Lng = "-102.453091";

}



}

我的主页是这样的:

using System.Collections.Generic;
using Xamarin.Forms.Maps;
using Xamarin.Forms;
using System.IO;
using Newtonsoft.Json;
using System;
using System.Globalization;
namespace Orbage
{
class MapPage : ContentPage
{
public MapPage()
{
CustomMap customMap = new CustomMap
{
MapType = MapType.Street
};
// ...
Content = customMap;
var json = File.ReadAllText("DATA");
NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberDecimalSeparator = ".";
provider.NumberGroupSeparator = ",";
double doubleVal = Convert.ToDouble("855.65", provider);
var places = JsonConvert.DeserializeObject<List<DATA>>(json);
foreach (var place in places)
{
CustomPin pin = new CustomPin
{
Type = PinType.Place,
Position = new Position(place.Lat,place.Lng),
Label = place.Label,
Address = place.Address,
Name = "Xamarin",
Url = "http://xamarin.com/about/"
};

customMap.CustomPins = new List<CustomPin> { pin };
customMap.Pins.Add(pin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
}
}
}
}

即使在这样做之后,我也会收到这个错误。我如何修复这个问题或对我的代码进行任何更改,这样就不需要加倍。如果我的代码看起来令人困惑,我基本上已经创建了自定义映射,而不是创建一百万个pin,而是在数据脚本中编写所有代码。当我尝试应用它时,我会遇到这个错误。有没有更好的方法可以避免加倍,或者你能告诉我如何转换它吗。请帮帮我。非常感谢!

此行出错:位置=新位置(place.Lat,place.Lng(,

DATA类中,LatLng被定义为字符串。您需要将它们转换为double,这正是Position需要的地方

Position = new Position(Double.Parse(place.Lat),Double.Parse(place.Lng)),

最新更新