簡體   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