Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步错误Xamarin iOS_C#_Ios_Xamarin_Xamarin.ios - Fatal编程技术网

C# 异步错误Xamarin iOS

C# 异步错误Xamarin iOS,c#,ios,xamarin,xamarin.ios,C#,Ios,Xamarin,Xamarin.ios,我在iOS开发方面比较新 我在Xamarin C上为iOS编写应用程序# 我想通过GET请求(JSON)解析URL中的文本 当我编写安卓应用程序时,我有这个代码,它工作正常 string url2 = "http://new.murakami.ua/?mkapi=getActions"; JsonValue json = await FetchAsync(url2); ParseAndDisplay(json); private async Task<JsonValue> Fet

我在iOS开发方面比较新

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

当我编写安卓应用程序时,我有这个代码,它工作正常

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



}
字符串url2=”http://new.murakami.ua/?mkapi=getActions";
JsonValue json=await-FetchAsync(url2);
ParseAndDisplay(json);
专用异步任务FetchAsync(字符串url)
{
//使用URL创建HTTP web请求:
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(新Uri(url));
request.ContentType=“application/json”;
request.Method=“GET”;
//将请求发送到服务器并等待响应:
使用(WebResponse=await request.GetResponseAsync())
{
//获取HTTP web响应的流表示形式:
使用(Stream=response.GetResponseStream())
{
//使用此流构建JSON文档对象:
JsonValue jsonDoc=wait Task.Run(()=>JsonObject.Load(stream));
//动态数据=JObject.Parse(jsonDoc[15].ToString);
//Console.Out.WriteLine(“响应:{0}”,jsonDoc[0].ToString);
//返回JSON文档:
返回jsonDoc;
}
}
}
私有void ParseAndDisplay(JsonValue json)
{
ImageView imagen=findViewBydResource.Id.image1);
TextView titlename=findviewbydresource.Id.title1);
//TextView datename=findViewBydResource.Id.date1);
imagen.Click+=委托
{
var intent358=新的意图(该,类型为(AkciiActivity1));
起始触觉(intent358);
};
标题名。单击+=委派
{
var intent359=新的意图(该,类型为(AkciiActivity1));
起始触觉(intent359);
};
JsonValue firstitem=json[0];
//Console.Out.WriteLine(firstitem[“post_title”].ToString());
titlename.Text=firstitem[“title”];
//datename.Text=firstitem[“date”];
var imageBitmap2=GetImageBitmapFromUrl(第一项[“img”]);
设置图像位图(图像位图2);
}
我尝试在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;
}

}
}
namespace murakami_kiev{
分部类ActionsViewController:UIViewController{
公共操作IEWController(IntPtr句柄):基本(句柄){
}
//aktions解析器开始
字符串url2=”http://new.murakami.ua/?mkapi=getActions";
JsonValue json=await-FetchAsync(url2);
私有异步任务FetchAsync(字符串url){
//使用URL创建HTTP web请求:
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(新Uri(url));
request.ContentType=“application/json”;
request.Method=“GET”;
//将请求发送到服务器并等待响应:
使用(WebResponse=await request.GetResponseAsync()){
//获取HTTP web响应的流表示形式:
使用(Stream=response.GetResponseStream()){
//使用此流构建JSON文档对象:
JsonValue jsonDoc=wait Task.Run(()=>JsonObject.Load(stream));
//动态数据=JObject.Parse(jsonDoc[15].ToString);
//Console.Out.WriteLine(“响应:{0}”,jsonDoc[0].ToString);
//返回JSON文档:
返回jsonDoc;
}
}
}
私有void ParseAndDisplay(JsonValue json){
ImageView imagen=FindViewById(Resource.Id.image1);
TextView titlename=FindViewById(Resource.Id.title1);
//TextView datename=FindViewById(Resource.Id.date1);
/*imagen.Click+=委托
{
var intent358=新的意图(该,类型为(AkciiActivity1));
起始触觉(intent358);
};
标题名。单击+=委派
{
var intent359=新的意图(该,类型为(AkciiActivity1));
起始触觉(intent359);
};*/
JsonValue firstitem=json[0];
//Console.Out.WriteLine(firstitem[“post_title”].ToString());
titlename.Text=firstitem[“title”];
//datename.Text=firstitem[“date”];
var imageBitmap2=GetImageBitmapFromUrl(第一项[“img”]);
设置图像位图(图像位图2);
}
//aktions解析器结束
公共覆盖无效ViewDidLoad(){
base.ViewDidLoad();
//加载视图后,通常从nib执行任何附加设置。
字符串操作first=“ааааааааааааааааа;
Actions001.Text=actionsfirst;
}
}
}
我有一个错误:

错误CS4033:
await'运算符只能在其包含方法标记为
async'修饰符(CS4033)时使用


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

您的问题在哪里

JsonValue json = await FetchAsync(url2);
这相当于在构造函数中调用
await

private JsonValue json;

public ActionsViewController(IntPtr handle): base(handle) {
    json = await FetchAsync(url2); // Error!
}
这是不正确的,因为您不能在构造函数上放置
async
,以允许在内部使用
wait

那么,在这种情况下你能做些什么呢?我同意:

最好的解决方案是承认下载和设计的异步性

换句话说,在下载数据时,决定应用程序的外观。让页面构造函数设置该视图,然后开始下载。下载完成后,更新页面以显示数据

写下这个

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

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;
    }
  }
}
  string actionsfirst = "Новая супер акция от мураками";
  Actions001.Text = actionsfirst;
}
public async override void ViewDidLoad(){
base.ViewDidLoad();
//加载视图后,通常从nib执行任何附加设置。
字符串url2=”http://new.murakami.ua/?mkapi=getActions";
JsonValue json=await-FetchAsync(url2);
私有异步任务FetchAsync(字符串url){
//使用URL创建HTTP web请求:
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(新Uri(url));
request.ContentType=“application/json”;
request.Method=“GET”;
//将请求发送到服务器并等待响应:
使用(WebResponse=await request.GetResponseAsync()){
//获取HTTP web响应的流表示形式:
使用(Stream=response.GetResponseStream()){
//使用此流构建JSON文档对象:
JsonValue jsonDoc=等待任务。运行(()=>Json