创建对象时尝试使用lat/lon设置地理坐标。C#WP8

创建对象时尝试使用lat/lon设置地理坐标。C#WP8,c#,windows-phone-8,visual-studio-2013,C#,Windows Phone 8,Visual Studio 2013,我正在使用来自API的json数据创建多个总线对象。在它们被创建之后,我想创建一个地理坐标来保存经度和纬度。我是C#的新手,似乎不明白为什么这不管用?提前感谢您的任何意见 class Bus { public string id { get; set; } public double longitude { get; set; } public double latitude { get; set; } public string

我正在使用来自API的json数据创建多个总线对象。在它们被创建之后,我想创建一个地理坐标来保存经度和纬度。我是C#的新手,似乎不明白为什么这不管用?提前感谢您的任何意见

 class Bus
{
        public string id { get; set; }
        public double longitude { get; set; }
        public double latitude { get; set; }
        public string route { get; set; }
        public string lastStop { get; set; }
        public GeoCoordinate busLocation { get; set; }



        public Bus()
        {
           GeoCoordinate busLocation = new GeoCoordinate(latitude, longitude);
         }
}

我不明白你的代码想做什么。检查
busLocation
是否为null没有用,在调用构造函数时它总是为null。我想您应该实例化
busLocation
属性:

public Bus()
{
    busLocation = new GeoCoordinate();
}
因此,以后可以为其
纬度
经度
属性赋值,例如:

myBus.busLocation.Latitude = 50d;
myBus.busLocation.Longitude = 50d;

我将其更改为尝试使用以下代码:
publicbus(){geocordinate busLocation=new geocordinate(纬度、经度);}
但是当我调用myBus.busLocation时,它是空的。您尝试的代码创建了名为
busLocation
的新局部变量,而不是为预定义的全局变量/属性赋值。从代码中删除地理坐标:
busLocation=新地理坐标(纬度、经度)