繁体   English   中英

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

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

我希望异步任务由应用程序执行,但要先显示内容,然后再启动异步任务。 当我使用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

我不建议将 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);
    });
  }

暂无
暂无

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

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