C# Xamarin API VK列表形成

C# Xamarin API VK列表形成,c#,json,linq,http,json.net,C#,Json,Linq,Http,Json.net,我正试图为一项工作做鉴定工作,但在Xamarin上实现代码时遇到了一些问题。 我有这样的类和函数。他们正在开发c#的控制台,但不是xamarin。我不知道该怎么办。他们只对Xamarin进行冷冻 using System.Collections.Generic; using System.Threading.Tasks; using System.Net.Http; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace rad

我正试图为一项工作做鉴定工作,但在Xamarin上实现代码时遇到了一些问题。 我有这样的类和函数。他们正在开发c#的控制台,但不是xamarin。我不知道该怎么办。他们只对Xamarin进行冷冻

using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace radacode.ActionForm
{
class ListMaker
{
    public static List<Country> GetCountryList()
    {
        List<Country> result=new List<Country>();
        Task<string> task =GetRequestAsync(@"http://api.vk.com/method/database.getCountries?need_all=1&v=5.60");
        JObject vk = JObject.Parse(task.GetAwaiter().GetResult());
        foreach (JObject jsonCountry in vk["response"]["items"])
            result.Add(JsonConvert.DeserializeObject<Country>(jsonCountry.ToString()));
        return result;
    }

    public static async Task<string> GetRequestAsync(string url)
    {
        using (var httpClient = new HttpClient())
            return await httpClient.GetStringAsync(url);
    }
    public class Country
    {
        public int Cid { get; set; }
        public string Title { get; set; }

        override public string ToString()
        {
            return Title;
        }
    }


}
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用System.Net.Http;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
名称空间radacode.ActionForm
{
类列表生成器
{
公共静态列表GetCountryList()
{
列表结果=新列表();
任务任务=GetRequestAsync(@)http://api.vk.com/method/database.getCountries?need_all=1&v=5.60");
JObject vk=JObject.Parse(task.GetAwaiter().GetResult());
foreach(vk[“响应”][“项目”]中的JObject jsonCountry)
Add(JsonConvert.DeserializeObject(jsonCountry.ToString());
返回结果;
}
公共静态异步任务GetRequestAsync(字符串url)
{
使用(var httpClient=new httpClient())
返回wait-httpClient.GetStringAsync(url);
}
公营国家
{
公共int-Cid{get;set;}
公共字符串标题{get;set;}
重写公共字符串ToString()
{
返回标题;
}
}
}

}

使用Xamarin表单时,最好使用可移植类项目,而不是共享类项目。在我看来

同样重要的是,静态不能特别用于异步调用。将Async视为类似于JAVA中的AsyncTask。他们没有阻拦

public class ListMaker
    {
        public List<Country> GetCountyList()
        {
            return GetCountryListAsync().Result;
        }

        private async Task<List<Country>> GetCountryListAsync()
        {
            var result = new List<Country>();
            var task =
                await GetRequestAsync(@"http://api.vk.com/method/database.getCountries?need_all=1&v=5.60");

            var vk = JObject.Parse(task);

            foreach (var jsonCountry in vk["response"]["items"])
                result.Add(JsonConvert.DeserializeObject<Country>(jsonCountry.ToString()));
            return result;
        }

        private async Task<string> GetRequestAsync(string url)
        {
            using (var httpClient = new HttpClient())
                return await httpClient.GetStringAsync(url);
        }

        public class Country
        {
            public int Cid { get; set; }
            public string Title { get; set; }

            public new string ToString()
            {
                return Title;
            }
        }
    }

另外,还可以寻找JSON.NET,它可以通过Nuget(在工具下)安装,可以为您执行序列化和反序列化,尽管它会为您添加100毫秒的时间。很抱歉,这是我关于stackoverflow的第一篇文章,我正试着在短时间内撰写。我添加了一些您强调的更正。我使用了newtonsoftjson。但是这些修正都不能解决我的问题。我冻结了Xamarin的程序。在控制台c#应用程序中使用此类,给出预期结果-国家/地区列表。我希望你能理解我。英语不是我的母语。将应用程序连接到internet时出现问题。程序在响应时冻结。我将键入此内容,您是否将Xamarin窗体与包含此代码的可移植类项目一起使用?我将Xamarin窗体与共享类项目一起使用
public async void TheLister()
{
    var listMaker = new ListMaker();
    var countryList = await listmaker.GetCountryListAsync();

    // Do something with countryList
}