gpt4 book ai didi

c# - 异步错误 Xamarin iOS

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:29 26 4
gpt4 key购买 nike

我是 iOS 开发新手。

我在 Xamarin C# 上为 iOS 编写应用程序我想通过 GET 请求 (JSON) 解析来自 URL 的文本

当我编写 Android 应用程序时,我有这段代码并且它可以工作

string url2 = "http://new.murakami.ua/?mkapi=getActions";
JsonValue json = await FetchAsync(url2);
ParseAndDisplay(json);
private async Task<JsonValue> FetchAsync(string url)
{
// 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:
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
//dynamic data = JObject.Parse(jsonDoc[15].ToString);
//Console.Out.WriteLine("Response: {0}", jsonDoc[0].ToString);


// Return the JSON document:
return jsonDoc;
}
}
}

private void ParseAndDisplay(JsonValue json)
{


ImageView imagen = FindViewById<ImageView>Resource.Id.image1);
TextView titlename = FindViewById<TextView>Resource.Id.title1);
//TextView datename = FindViewById<TextView>Resource.Id.date1);
imagen.Click += delegate
{
var intent358 = new Intent(this, typeof(AkciiActivity1));
StartActivity(intent358);
};
titlename.Click += delegate
{
var intent359 = new Intent(this, typeof(AkciiActivity1));
StartActivity(intent359);
};
JsonValue firstitem = json[0];
//Console.Out.WriteLine(firstitem["post_title"].ToString());

titlename.Text = firstitem["title"];
//datename.Text = firstitem["date"];
var imageBitmap2 = GetImageBitmapFromUrl(firstitem["img"]);
imagen.SetImageBitmap(imageBitmap2);



}

我尝试在 iOS 中使用这段代码

我有这个代码

namespace murakami_kiev {

partial class ActionsViewController: UIViewController {
public ActionsViewController(IntPtr handle): base(handle) {
}
//aktions parser begin

string url2 = "http://new.murakami.ua/?mkapi=getActions";
JsonValue json = await FetchAsync(url2);

private async Task < JsonValue > FetchAsync(string url) {
// 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:
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
//dynamic data = JObject.Parse(jsonDoc[15].ToString);
//Console.Out.WriteLine("Response: {0}", jsonDoc[0].ToString);
// Return the JSON document:
return jsonDoc;
}
}
}

private void ParseAndDisplay(JsonValue json) {
ImageView imagen = FindViewById < ImageView > (Resource.Id.image1);
TextView titlename = FindViewById < TextView > (Resource.Id.title1);
//TextView datename = FindViewById<TextView>(Resource.Id.date1);
/*imagen.Click += delegate
{
var intent358 = new Intent(this, typeof(AkciiActivity1));
StartActivity(intent358);
};
titlename.Click += delegate
{
var intent359 = new Intent(this, typeof(AkciiActivity1));
StartActivity(intent359);
};*/
JsonValue firstitem = json[0];
//Console.Out.WriteLine(firstitem["post_title"].ToString());


titlename.Text = firstitem["title"];
//datename.Text = firstitem["date"];
var imageBitmap2 = GetImageBitmapFromUrl(firstitem["img"]);
imagen.SetImageBitmap(imageBitmap2);
}

//aktions parser end
public override void ViewDidLoad() {
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.

string actionsfirst = "Новая супер акция от мураками";
Actions001.Text = actionsfirst;
}

}
}

我有这个错误:

Error CS4033: The await' operator can only be used when its containing method is marked with theasync' modifier (CS4033)

我需要在哪里写异步,或者我的错误是什么?

最佳答案

你的问题在于行

JsonValue json = await FetchAsync(url2);

这相当于在构造函数中调用 await:

private JsonValue json;

public ActionsViewController(IntPtr handle): base(handle) {
json = await FetchAsync(url2); // Error!
}

这是不正确的,因为您不能将 async 放在构造函数上以允许在内部使用 await

那么,在这种情况下你能做什么呢? This topic was already discussed on SO我同意Stephen Cleary answer :

The best solution is to acknowledge the asynchronous nature of the download and design for it.

In other words, decide what your application should look like while the data is downloading. Have the page constructor set up that view, and start the download. When the download completes update the page to display the data.

关于c# - 异步错误 Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com