【发布时间】:2020-11-28 11:57:40
【问题描述】:
我有一个非常简单的代码和一些非常简单的文件。
我创建了一个 C# 文件来插入一些数据并从中制作一组引脚,但是当我这样做时,它给了我这个错误:
错误 CS0246 找不到类型或命名空间名称“Places”(您是否缺少 using 指令或程序集引用?)
我的代码:
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)));
}
}
}
}
我猜这就是所有需要的代码。我希望这已经足够了。
我参考了以下答案:
【问题讨论】:
-
我认为应该是 Data 而不是 Places in line DeserializeObject
- >(json);
-
我试过它不起作用只会触发更多错误。还有什么我能做的请告诉我
-
-
首先,在我的回复中
Place是您需要编写的自定义模型。其次,在您的代码中,您正在初始化CustomPinsinside 循环,这是不对的。