Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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# RestSharp不知道如何调用回调_C#_Xamarin_Restsharp - Fatal编程技术网

C# RestSharp不知道如何调用回调

C# RestSharp不知道如何调用回调,c#,xamarin,restsharp,C#,Xamarin,Restsharp,我正在使用restsharp从我正在做的API中获取数据,我想创建一个名为ApiInterface的类,该类将在Xamarin Android、iOS、Windows中使用。。。调用API 这个类有它的RestClient和函数,它们将从代码的任何部分调用,因为它是一个单例 例如,我将有一个MainActivity.cs类似的东西。调用我的getData函数并处理我收到的数据 Button buttonListaIntereses = FindViewById<Button> (Re

我正在使用restsharp从我正在做的API中获取数据,我想创建一个名为ApiInterface的类,该类将在Xamarin Android、iOS、Windows中使用。。。调用API

这个类有它的RestClient和函数,它们将从代码的任何部分调用,因为它是一个单例

例如,我将有一个MainActivity.cs类似的东西。调用我的getData函数并处理我收到的数据

Button buttonListaIntereses = FindViewById<Button> (Resource.Id.myButton);

buttonListaIntereses.Click += delegate {
    ApiInterface.Instance.getData(response2=>
    {
        Intent displayMessage = new Intent(this, typeof(DisplayMessage));
        //Put info in Extra for the new screen.
        displayMessage.PutExtra("content", response2.Content);
        StartActivity(displayMessage);
    });
};
但是在APIInterface中,我希望获得像cookies这样的公共数据

public async void getData(Action <IRestResponse> onDone)
{           
    RestRequest request = new RestRequest("getData", Method.GET);

   //Execute ASYNC the rest request
    m_Client.ExecuteAsync (request, response =>
    {
        //Do my stuff with headers.
        string lCookie = response.Headers.ToList().Find(x => x.Name == "Cookie").Value.ToString();
       //Execute the OnDone
       onDone();
    });
}
我的问题是我不确定如何在getData中执行OnOne和/或如何调用getData函数

谢谢

操作参数onDone采用IRestReponse类型的参数:

操作参数onDone采用IRestResponse类型的参数:


我将不再使用回调,而是利用C的异步/等待特性

buttonListaIntereses.Click += async delegate {
    var response = await ApiInterface.Instance.getData();
    LaunchResponseActivity(response);
};

public void LaunchResponseActivity(IRestResponse response)
{
    Intent displayMessage = new Intent(this, typeof(DisplayMessage));
    //Put info in Extra for the new screen.
    displayMessage.PutExtra("content", response.Content);
    StartActivity(displayMessage);
}

public async Task<IRestResponse> getData()
{           
    RestRequest request = new RestRequest("getData", Method.GET);

    var cancellationTokenSource = new CancellationTokenSource();

    var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);

    //Do my stuff with headers.
    string lCookie = restResponse.Headers.ToList().Find(x => x.Name == "Cookie").Value.ToString();

    return restResponse;
}

我将不再使用回调,而是利用C的异步/等待特性

buttonListaIntereses.Click += async delegate {
    var response = await ApiInterface.Instance.getData();
    LaunchResponseActivity(response);
};

public void LaunchResponseActivity(IRestResponse response)
{
    Intent displayMessage = new Intent(this, typeof(DisplayMessage));
    //Put info in Extra for the new screen.
    displayMessage.PutExtra("content", response.Content);
    StartActivity(displayMessage);
}

public async Task<IRestResponse> getData()
{           
    RestRequest request = new RestRequest("getData", Method.GET);

    var cancellationTokenSource = new CancellationTokenSource();

    var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);

    //Do my stuff with headers.
    string lCookie = restResponse.Headers.ToList().Find(x => x.Name == "Cookie").Value.ToString();

    return restResponse;
}

嗨,Andrés,我没有试过你的代码,我是新使用async/await c功能的人,它们的优点是什么?你是说最好改变xamarin的所有界面及其导航流程。嗨,Andrés,我没有试过你的代码,我是新使用async/await c功能的人,它们的优点是什么?你是说最好更改xamarin的所有界面及其导航流程。谢谢lenkan!我被它困住了!:谢谢lenkan!我被它困住了!: