有没有办法可以在创建时启动异步任务,但不能创建异步创建Xamarin

时间:2016-03-12 06:51:10

标签: c# android asynchronous xamarin async-await

我希望应用程序执行异步任务,但首先显示内容,然后启动异步任务。当我使用protected async override void OnCreate(Bundle bundle)时,在执行异步任务之前,我无法看到按钮或屏幕上的内容等内容。这完全使得使用异步任务毫无用处。

我可以用Button.Click来实现它。但话说回来,这不是我想要的。我想在OnCreate设置所有视图后立即启动异步任务。也许问题在于protected async override void OnCreate(Bundle bundle)

还有其他方法可以启动任务onCreate吗?

这是我的代码。

[Activity(Label = "NewsDetails")]
public class NewsDetails : Activity {
  protected async override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.NewsDetails);
    TextView Title = FindViewById<TextView>(Resource.Id.textTitle);
    TextView Source = FindViewById<TextView>(Resource.Id.textSource);
    WebView webDisplay = FindViewById<WebView>(Resource.Id.webDisplay);
    string thisid=Intent.GetStringExtra ("id");
    string url = "http://MyApiUrl/";
    url = url + thisid;
    var result = await GetNewsAsync(url);
    Title.Text = result.GetString("Title");
    Source.Text = result.GetString("Source");
    string ExternalReference = result.GetString("ExternalReference");
    webDisplay.Settings.JavaScriptEnabled = true;
    webDisplay.LoadUrl(ExternalReference);
  }
  private async Task<JSONObject> GetNewsAsync(string url) {
    // Create an HTTP web request using the URL:
    // Send the request to the server and wait for the response:
    // Return some JSONObject after async task here
    // Create an HTTP web request using the URL:
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
    request.ContentType = "application/json";
    request.Method = "GET";
    // Send the request to the server and wait for the response:
    using (WebResponse response = await request.GetResponseAsync()) {
      // Get a stream representation of the HTTP web response:
      using (Stream stream = response.GetResponseStream()) {
        // Use this stream to build a JSON document object:
        JSONObject jsonResponse;
        Stream jsonDoc = stream;
        Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        String responseString = reader.ReadToEnd();
        jsonResponse = new JSONObject(responseString);
        JSONObject jResult = jsonResponse.GetJSONObject("Result");
        return jResult;
      }
    }
  }
}

我想知道是否有什么我做错了,或者是否有完全不同的方法来实现我想做的事情。

欢迎代码中的任何建议。

编辑:async Task<JSONObject> GetNewsAsync的代码。

1 个答案:

答案 0 :(得分:0)

我不建议将OnCreate方法标记为异步。

尝试类似的东西:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.NewsDetails);

    TextView Title = FindViewById<TextView>(Resource.Id.textTitle);
    TextView Source = FindViewById<TextView>(Resource.Id.textSource);
    WebView webDisplay = FindViewById<WebView>(Resource.Id.webDisplay);
    webDisplay.Settings.JavaScriptEnabled = true;

    string thisid=Intent.GetStringExtra ("id");
    string url = "http://MyApiUrl/";
    url = url + thisid;

    GetNewsAsync(url).ContinueWith(t=>
    {
        var result = t.Result;

        Title.Text = result.GetString("Title");
        Source.Text = result.GetString("Source");
        string ExternalReference = result.GetString("ExternalReference");

        webDisplay.LoadUrl(ExternalReference);
    });
  }