【问题标题】:How can Xamarin.Forms read json from WCF?Xamarin.Forms 如何从 WCF 读取 json?
【发布时间】:2019-06-26 11:39:39
【问题描述】:

我正在尝试使用地图和图钉创建我的第一个 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://80.102.51.381:101/Service1.svc/ToEraseGetCoordinates(用于数据表的 xml 表示)

对于 JSON: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 并绘制引脚。

【问题讨论】:

  • 您使用的是哪种 WCF 绑定?网址?如果是这样,json 将非常容易与 httpclient 和 newtonsoft 一起使用。此代码可能有用:github.com/dotnet-architecture/eShopOnContainers/tree/dev/src/…
  • 谢谢,但问题不在于如何转换为 json。我已经使用 newtonsoft 转换为json
  • 如果你创建一个 REST 服务而不是使用 WCF,你的生活会轻松很多

标签: c# wcf xamarin xamarin.forms visual-studio-2017


【解决方案1】:

earthquake map sample 基本上可以满足您的需求(遗憾的是,它有点老了)。

基本上你想下载你的Json(例如this class

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

最后只是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);
    }
});

最初这些引脚无法进行数据绑定,这就是它循环添加它们的原因。不确定从那以后 Xamarin.Forms 中是否添加了数据绑定 ping 的功能。

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

请参阅Maps docsmaps custom renderers 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-05
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多