I have a class which looks like this:
internal class MyClass
: AsyncTask<string, int, List<List<Dictionary<string, string>>>>
{
protected override void OnPostExecute(List<List<Dictionary<string, string>>> result)
{
}
protected override List<List<Dictionary<string, string>>>
RunInBackground(params string[] jsonData)
{
var routes = new List<List<Dictionary<string, string>>>();
return routes;
}
}
Where AsyncTask
belongs to Android.OS
And find that OnPostExecute
is never executed.
I have other classes which also inherit from AsyncTask
(specifically, AsyncTask<string, string, string>
, but these are working properly - ie OnPostExecute
is called when expected.
What causes this, and how can I get around it?
After checking this question , I suspected this may have something to do with the fact that RunInBackground
returns a .NET
type ( List
).
After messing around some more I found that this the right way to do this is probably to define the following 2 methods:
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
}
protected override void OnPostExecute(List<List<Dictionary<string, string>>> result)
{
}
Where OnPostExecute(Java.Lang.Object result)
is called first, and passes result
into the other OnPostExecute
, which at some point converts Object
to the required List
.
So I added a second OnPostExecute
method, which does get called:
protected override void OnPostExecute(Java.Lang.Object result)
{
}
After checking result
in the debugger, I could see the values I needed in the Instance
property, which I can now retrieve with:
var propertyInfo = result.GetType().GetProperty("Instance");
var newResult =
propertyInfo.GetValue(result, null) as List<List<Dictionary<string, string>>>;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.