使用xamarin跨平台列表视图中无法打印的json消息 [英] Cannot print the json message in listview using xamarin cross platform

查看:231
本文介绍了使用xamarin跨平台列表视图中无法打印的json消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用xamarin跨平台的发展,我想从网址打印JSON消息,并在列表视图打印。我写code没有错误,但有一些错误。我将无法工作。对于只是一个verfication输出只有我在打印输出屏幕的按钮。我将显示,但是在ListView没有打印。请recorrect我的code。

 静态的ListView列表视图;
    静态Button按钮;
    公众的MainPage()
    {
        列表视图=新的ListView(){的rowHeight = 40};
        按钮=新按钮(){文字=再次搜索};
        VAR堆栈=新StackLayout()
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            儿童= {按钮,列表视图},
        };
        this.Content =栈;
        的GetData();
    }
     异步静态无效的GetData()
    {
        WeatherReport解析度=新WeatherReport();
        尝试
        {
            字符串内容;
            字符串的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 =新的HttpClient();
            内容=等待hc.GetStringAsync(URL);
            RES = JsonConvert.DeserializeObject< WeatherReport>(内容);
            listview.ItemsSource = res.list;
        }
        赶上(System.Exception的sysExc)
        {
            //做一些事情记录此错误。
            扔;
        }
    }
 公共类WeatherReport
 {
    公共WeatherReport()
    {
        名单=新名单,LT; WeatherReport>();
    }
    [JsonProperty(属性名=鳕鱼)]
    公共字符串鳕鱼{搞定;组; }
    [JsonProperty(属性名=消息)
    公共字符串消息{搞定;组; }
    [JsonProperty(属性名=CNT)]
    公众诠释CNT {搞定;组; }
    [JsonProperty(属性名=名单)]
    公开名单< WeatherReport>列表{搞定;组; }


解决方案

首先,你不应该使用异步无效除了事件处理器,这是不好的做法。其次,的GetData()是一个异步方法,但是你是在构造函数中调用同步它。我会建议重新设计这一点,以便它是异步一路过关斩将。你可能有,因为你正在等待code继续之前完成的问题。在code其他的一切似乎的 的确定。

下面是一些额外的资源可能与异步帮助/等待。
http://msdn.microsoft.com/en-us/magazine/jj991977.aspx
<一href=\"http://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void\">async/await - 当返回任务VS空白

Using a xamarin cross platform development to i want print a json message from url and print in listview. I write code without error but with some mistakes. I will not work. For just a verfication output only i print the button in the output screen. I will shown but the listview was not print. Please recorrect my code.

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

解决方案

First, you shouldn't use async void except for eventhandlers, it is bad practice. Second, GetData() is an asynchronous method, however you are calling it synchronously in a constructor. I would advise redesigning this a bit so that it is async all the way through. You may be having issues since you are waiting for the code to finish before continuing on. Everything else in the code appears ok.

Here are some additional resources that might help with async/await. http://msdn.microsoft.com/en-us/magazine/jj991977.aspx, async/await - when to return a Task vs void?

这篇关于使用xamarin跨平台列表视图中无法打印的json消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆