使用xamarin跨平台开发我想从url打印json消息并在listview中打印。我编写代码没有错误,但有一些错误。我不会工作。仅对于验证输出,我只在输出屏幕中打印按钮。我会展示但是列表视图不是打印的。请记下我的代码。
static ListView listview;
static Button button;
public MainPage()
{
listview = new ListView() { RowHeight = 40 };
button = new Button() { Text = "search Again" };
var stack = new StackLayout()
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { button, listview },
};
this.Content = stack;
GetData();
}
async static void GetData()
{
WeatherReport res = new WeatherReport();
try
{
string contents;
string Url = String.Format("http://23.253.66.248:8080/icc/api/venue/search/? lat=39.540544&lon=-104.866115&appkey=123Chesse&restName=MyMacChennai&organization=MyMacChennai");
HttpClient hc = new HttpClient();
contents = await hc.GetStringAsync(Url);
res = JsonConvert.DeserializeObject<WeatherReport>(contents);
listview.ItemsSource = res.list;
}
catch (System.Exception sysExc)
{
// Do something to log this error.
throw;
}
}
public class WeatherReport
{
public WeatherReport()
{
list = new List<WeatherReport>();
}
[JsonProperty(PropertyName = "cod")]
public string cod { get; set; }
[JsonProperty(PropertyName = "message")]
public string message { get; set; }
[JsonProperty(PropertyName = "cnt")]
public int cnt { get; set; }
[JsonProperty(PropertyName = "list")]
public List<WeatherReport> list { get; set; }
答案 0 :(得分:0)
首先,除了事件处理程序之外,你不应该使用async void
,这是不好的做法。其次,GetData()
是一个异步方法,但是你在构造函数中同步调用它。我建议稍微重新设计一下,以便它一直是异步的。您可能遇到问题,因为您在等待代码完成后再继续。代码中的其他所有内容都显示为确定。
以下是一些可能有助于async / await的其他资源。 http://msdn.microsoft.com/en-us/magazine/jj991977.aspx, async/await - when to return a Task vs void?