繁体   English   中英

Xamarin.Forms 如何从 WCF 读取 json?

[英]How can Xamarin.Forms read json from WCF?

我正在尝试使用地图和图钉创建我的第一个Xamarin.Forms移动应用程序,所以请耐心等待。

我正在尝试向地图添加图钉。 我使用此代码添加一个引脚:

map = new Map { 
    IsShowingUser = true,
    HeightRequest = 100,
    WidthRequest = 960,
    VerticalOptions = LayoutOptions.FillAndExpand
};

map.MoveToRegion (MapSpan.FromCenterAndRadius (
    new Position (36.9628066,-122.0194722), Distance.FromMiles (3)));

var position = new Position(36.9628066,-122.0194722);
var pin = new Pin {
    Type = PinType.Place,
    Position = position,
    Label = "Santa Cruz",
    Address = "custom detail info"
};
map.Pins.Add(pin);

现在,我想从 tsql 表中添加几个引脚,而不是只添加一个引脚。

所以我创建了一个返回坐标列表的WCF service 一个返回json和其他回报datatable

public DataTable ToEraseGetCoordinates()
{
    string sqlQuery = "select lat,lon from MyStores";
    string connString = GetConnString();
    SqlDatabase sqlDatabase = new SqlDatabase(connString);
    DataSet result = sqlDatabase.ExecuteDataSet(CommandType.Text, sqlQuery);
    return result.Tables[0];
}
public System.IO.Stream ToEraseGetCoordinatesJson()
{
    string sqlQuery = "select lat,lon from MyStores";
    string connString = GetConnString();
    SqlDatabase sqlDatabase = new SqlDatabase(connString);
    DataSet result = sqlDatabase.ExecuteDataSet(CommandType.Text, sqlQuery);
    return ConvertToJson(result.Tables[0]);
}

我像这样调用 WCF:http: http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinates : http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinates (用于数据表的 xml 表示)

对于 JSON:http: http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinatesJson : http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinatesJson ,它返回:

{"lat":25.7616,"lon":-80.1917},{"lat": 28.5383,"lon":-81.3792}

我的问题是:接下来我该怎么做才能让我的 Xamarin.Form 读取此内容?

无论返回类型如何,我都不知道 Xamarin 将如何消耗 WCF 并绘制引脚。

这个地震地图样本基本上可以满足您的需求(遗憾的是它有点旧)。

基本上你想下载你的 Json(例如在这个类中

// your http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinatesJson would go here
var response = await client.GetAsync("earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt");
var earthquakesJson = response.Content.ReadAsStringAsync().Result;

那么你需要转换 Json - 但你说你已经这样做了

var rootobject = JsonConvert.DeserializeObject<Rootobject>(earthquakesJson);

最后只需创建并将图钉添加到地图

var earthquakes = await webSvc.GetEarthquakesAsync();
Xamarin.Forms.Device.BeginInvokeOnMainThread( () => {
    Debug.WriteLine("found " + earthquakeString.Length + " earthquakes");
    label.Text = earthquakeString.Length + " earthquakes";
    foreach (var earthquake in earthquakes)
    {
        var p = new Pin();
        p.Position = new Position(earthquake.lat, earthquake.lng);
        p.Label = "Magnitude: " + earthquake.magnitude;
        m.Pins.Add(p);
    }
});

最初引脚无法进行数据绑定,这就是为什么它会循环添加它们。 不确定是否已将数据绑定 ping 的功能添加到 Xamarin.Forms 中。

您可能想要跟踪纬度/经度,以便您可以计算一个新区域来设置地图视图,默认情况下显示您添加的所有图钉...

有关详细信息,请参阅地图文档地图自定义渲染器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM