简体   繁体   English

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

[英]Is there a way I can start async task on create but not async oncreate Xamarin

I want the asynchronous task to be performed by the application, but the content is to be displayed first, then start the async task.我希望异步任务由应用程序执行,但要先显示内容,然后再启动异步任务。 When I use protected async override void OnCreate(Bundle bundle) , I don't get to see the contents like button or the content on the screen before the async task is performed.当我使用protected async override void OnCreate(Bundle bundle) ,在执行异步任务之前,我看不到按钮等内容或屏幕上的内容。 This completely makes it useless to use async task.这完全使使用异步任务变得毫无用处。

I could achieve it with Button.Click .我可以用Button.Click实现它。 But then again, it is not what I want.但话又说回来,这不是我想要的。 I want to start the async task immediately after OnCreate sets all the views.我想在OnCreate设置所有视图后立即启动异步任务。 Maybe the problem is with protected async override void OnCreate(Bundle bundle) .也许问题在于protected async override void OnCreate(Bundle bundle)

Is there any other way around to start the task onCreate ?有没有其他方法可以启动onCreate任务?

Here is my code.这是我的代码。

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

I would like to know if there is something I am doing wrong or if there is a completely different method to achieve what I am trying to do.我想知道我是否做错了什么,或者是否有一种完全不同的方法来实现我想要做的事情。

Any suggestions in the code are welcome.欢迎代码中的任何建议。

Edit: code for async Task<JSONObject> GetNewsAsync .编辑: async Task<JSONObject> GetNewsAsync

I would not recommend to mark the OnCreate method as async.我不建议将 OnCreate 方法标记为异步。

Try something like that:尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM