我有一个非常简单的代码和非常简单的文件。我创建了一个C#文件来插入数据并从中收集针脚,但是当我这样做时,它给了我这个错误:
Error CS0246 The type or namespace name 'Places' could not be found (are you missing a using directive or an assembly reference?)
我的代码:
DATA.CS
using System;
using System.Collections.Generic;
using System.Text;
namespace Orbage
{
class DATA
{
string Label = "USA",
Address = "This is the US",
Lat = "40.060407",
Lng = "-102.453091";
}
}
MapPage.cs(这是错误)
using System.Collections.Generic;
using Xamarin.Forms.Maps;
using Xamarin.Forms;
using System.IO;
using Newtonsoft.Json;
namespace Orbage
{
class MapPage : ContentPage
{
public MapPage()
{
CustomMap customMap = new CustomMap
{
MapType = MapType.Street
};
// ...
Content = customMap;
var json = File.ReadAllText("DATA");
var places = JsonConvert.DeserializeObject<List<Places>>(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)));
}
}
}
}
我猜这是所有需要的代码。我希望这已经足够了我也从这个答案中获取参考:反序列化json对象xamarin android c#和这个:我如何简化地图中的图钉
非常感谢!
您的代码中有很多问题。
我正在尝试仅解决与您提出的问题有关的问题。
应该DATa
不是Place
,您需要将属性声明为,public
并从更改lat
为Lat
等。
class DATA
{
public string Label = "USA";
public string Address = "This is the US";
public string Lat = "40.060407";
public string Lng = "-102.453091";
}
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,
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句