简体   繁体   English

Xamarin.Forms 如何从 WCF 读取 json?

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

I'm trying to create my first Xamarin.Forms mobile app with a map and pins, so please bear with me.我正在尝试使用地图和图钉创建我的第一个Xamarin.Forms移动应用程序,所以请耐心等待。

I'm trying to add pins to the map.我正在尝试向地图添加图钉。 I use this code to add one pin:我使用此代码添加一个引脚:

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);

Now, instead of adding just one pin, I'd like to add several pins from a tsql table.现在,我想从 tsql 表中添加几个引脚,而不是只添加一个引脚。

So I created a WCF service that returns a list of coordinates.所以我创建了一个返回坐标列表的WCF service One returns a json and the other returns a datatable :一个返回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]);
}

I invoke the WCF like so: http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinates (for an xml representation of the datatable)我像这样调用 WCF:http: http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinates : http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinates (用于数据表的 xml 表示)

For the JSON: http://80.102.51.381:101/Service1.svc/ToEraseGetCoordinatesJson , which returns this:对于 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}

My question is: what do I next so that my Xamarin.Form reads this?我的问题是:接下来我该怎么做才能让我的 Xamarin.Form 读取此内容?

Regardless of the return type, I don't know how Xamarin will consume the WCF and draw the pins.无论返回类型如何,我都不知道 Xamarin 将如何消耗 WCF 并绘制引脚。

This earthquake map sample does basically what you want (it's a bit old, sadly).这个地震地图样本基本上可以满足您的需求(遗憾的是它有点旧)。

Basically you want to download your Json (eg in this class )基本上你想下载你的 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;

then you need to convert the Json - but you said you've already done that那么你需要转换 Json - 但你说你已经这样做了

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

lastly just create and add the pins to the map最后只需创建并将图钉添加到地图

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);
    }
});

Originally the pins weren't able to be databound, which is why it loops through to add them.最初引脚无法进行数据绑定,这就是为什么它会循环添加它们。 Not sure if the ability to databind pings has been added to Xamarin.Forms since.不确定是否已将数据绑定 ping 的功能添加到 Xamarin.Forms 中。

You'll probably want to keep track of the lat/longs so you can calculate a new region to set the maps view, to default to showing all the pins you added...您可能想要跟踪纬度/经度,以便您可以计算一个新区域来设置地图视图,默认情况下显示您添加的所有图钉...

See the Maps docs and maps custom renderers for more info.有关详细信息,请参阅地图文档地图自定义渲染器

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

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