xamarin表格被调用

本文关键字:调用 表格 xamarin | 更新日期: 2023-09-27 18:01:04

嘿,伙计们,我是Xamarin的新手,我认真地发现了一些基本问题,这真的让我很沮丧。你能帮我走第一步吗。我正在进行一个get调用,我想返回字符串,但我有一个只能在async方法中使用的'await'运算符。如何返回字符串我真的没有xamarin.forms 的经验

 public class App : Application
{
    public App()
    {
        // The root page of your application
        MainPage = new ContentPage
        {
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    new Label {
                        HorizontalTextAlignment = TextAlignment.Center,
                        Text = "Welcome to Xamarin Forms!"
                    }
                }
            }
        };
         string s = await MakeGetRequest("https://gig.com.qa/AndroidWebService/Services/IPhoneMarketInfo.aspx?id=1");
    }

    public object DeserializeContent<T>(String stringToDeserialize)
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<T>));
        MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(stringToDeserialize));
        return (List<T>)ser.ReadObject(stream);
    }
    public async Task<string> MakeGetRequest(string url)
    {
        var request = HttpWebRequest.Create(url);
        HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                var content = reader.ReadToEnd();
                if (string.IsNullOrWhiteSpace(content))
                {
                    return response.StatusCode + "Response contained an empty body...";
                }
                else
                {
                    return content;
                }
            }
        }
        else
        {
            return response.StatusCode.ToString();
        }
    }

    public String SerializeContent(Object objectToSerialize)
    {
        return JsonConvert.SerializeObject(objectToSerialize);
    }

    public async Task<string> MakePostRequest(string url, string serializedDataString, bool isJson = true)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        if (isJson)
            request.ContentType = "application/json";
        else
            request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        var stream = await request.GetRequestStreamAsync();
        using (var writer = new StreamWriter(stream))
        {
            writer.Write(serializedDataString);
            writer.Flush();
            writer.Dispose();
        }
        var response = await request.GetResponseAsync();
        var respStream = response.GetResponseStream();
        using (StreamReader sr = new StreamReader(respStream))
        {
            return sr.ReadToEnd();
        }
    }
    protected override void OnStart()
    {
        // Handle when your app starts
    }
    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }
    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

xamarin表格被调用

由于不能在构造函数中等待,因此最好将其加载到OnStart((中

protected override async void OnStart()
{
   string s = await MakeGetRequest("https://gig.com.qa/AndroidWebService/Services/IPhoneMarketInfo.aspx?id=1");
}