android - Get an image asynchronously performs an error -


in xamarin, have gridview adapter sets images each gridview item. gridview adapter loaded fragment part of viewpager.

each gridview item has imageview displays image. have code displays image correctly when downloading image synchronously, however, not asynchronously.

here code:

public override view getview (int position, view convertview, viewgroup parent) {     view view = convertview;     if (view == null)         view = context.layoutinflater.inflate(resource.layout.gridviewwithimageandtextitem, gridview, false);     imgicon =  view.findviewbyid<imageview> (resource.id.image);     if (gridviewtems[position].useresourceasthumbnail)      {         imgicon.setimageresource (gridviewtems[position].imageresourceid);     } else {         //code work         imgicon.setimagebitmap (getimagebitmapfromurl (gridviewtems[position].thumbnailwebaddress));         //code not work         downloadasyncbitmap (gridviewtems[position].thumbnailwebaddress, imgicon);     }     view.findviewbyid<textview> (resource.id.myimageviewtext).text = gridviewtems[position].text;     return view; }  async void downloadasyncbitmap(string url, imageview imageview) {     var webclient = new webclient ();     var uri = new uri (url);     byte[] bytes = null;     try     {         bytes = await webclient.downloaddatataskasync(uri);     }     catch(taskcanceledexception){         console.writeline ("task canceled!");         return;     }     catch(exception e){         console.writeline (e.tostring());         return;     }      bitmap bitmap = await bitmapfactory.decodebytearrayasync (bytes, 0, bytes.length);     imageview.setimagebitmap (bitmap); }  private bitmap getimagebitmapfromurl(string url) {     bitmap imagebitmap = null;      using (var webclient = new webclient())     {         var imagebytes = webclient.downloaddata(url);         if (imagebytes != null && imagebytes.length > 0)         {             imagebitmap = bitmapfactory.decodebytearray(imagebytes, 0, imagebytes.length);         }     }      return imagebitmap; } 

this application output when trying use asynchronous code:

system.missingmethodexception: constructor on type 'system.diagnostics.debuggerbrowsableattribute' not found.    @ system.runtimetype.createinstanceimpl(bindingflags bindingattr, binder binder, object[] args, cultureinfo culture, object[] activationattributes, stackcrawlmark& stackmark)    @ system.activator.createinstance(type type, bindingflags bindingattr, binder binder, object[] args, cultureinfo culture, object[] activationattributes)    @ system.activator.createinstance(type type, object[] args)    @ mono.debugging.soft.softdebuggeradaptor.buildattribute[t](customattributedatamirror attr)    @ mono.debugging.soft.softdebuggeradaptor.getattribute[t](customattributedatamirror[] attrs)    @ mono.debugging.soft.softdebuggeradaptor.ongettypedisplaydata(evaluationcontext ctx, object type) 

this exact code works in application of mine, not in application google maps. there permission missing?

may please have set images asynchronously?

thanks in advance

edit

thank resource, have read , understand more async , await operators.

here current code:

async task<bitmap> downloadbitmapasync(string url) {     var webclient = new httpclient ();     var uri = new uri (url);     byte[] bytes = null;      try     {         bytes = await webclient.getbytearrayasync(uri);     }     catch(taskcanceledexception){         console.writeline ("task canceled!");         return null;     }     catch(exception e){         console.writeline (e.tostring());         return null;     }      bitmap bitmap = await bitmapfactory.decodebytearrayasync (bytes, 0, bytes.length);     return (bitmap); } 

and calling code , setting bitmap code:

downloadbitmapasync(gridviewitems[position].thumbnailimagewebaddress)     .continuewith(continuation => { imgicon.setimagebitmap (continuation.result); }); 

the gridview displays, yet none of imageview objects have images in them.

is code correct?

you using async-await in wrong way.

first of all, should never return "void" async task. so, downloadasyncbitmap (proper name convention downloadbitmapasync) must return task (equivalent of void), or better yet, task<bitmap>.

second, call task in wrong way. there approach named "async way", means should return task way top-level interface method (command, clickevent or getview). if event returning void pretty simple run task asynchronously:

public async override void onstart() {     await dosomethingasync(); } 

you should understand, async-wait syntax sugar around tasks , continuations. so, if method can't return void (like getview) can use continuewith construction.

in case proper way start async task getview is:

downloadasyncbitmap(gridviewtems[position].thumbnailwebaddress)     .continuewith(continuation => { imageview.setimagebitmap (continuation.result); }); 

i recommend study async-await little more http://msdn.microsoft.com/en-us/library/vstudio/hh191443(v=vs.110).aspx


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -