Make your Xamarin.Android app Responsive




Something i saw in novices mobile developers are that all the instructions they made they put it on the main thread so when you make that in a long hard work their apps goes down.


In C# there are two ways to do this

1. Threads (Parallels)

ParallelButton.Click += (sender, e) => {
    showHUD = ckbHUD.Checked;
    Task.Factory.StartNew(()=>{
     if(showHUD)
      AndHUD.Shared.Show(this, "Downloading Image via Parallel", -1, MaskType.Clear);
     var httpClient = new HttpClient();
     byte[] imageBytes  = httpClient.GetByteArrayAsync("http://upload.wikimedia.org/wikipedia/commons/6/66/Big_size_chess_6759_CRI_08_2009_Langosta_Beach.jpg").Result;
     return imageBytes; 
    }).ContinueWith(res=>{
     string documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
     string localPath = System.IO.Path.Combine (documents, "image.png");
     File.WriteAllBytes (localPath, res.Result);
     var localImage = new Java.IO.File (localPath);
     if (localImage.Exists ()) {
      var imgBitmap = BitmapFactory.DecodeFile (localImage.AbsolutePath);
      RunOnUiThread(()=>{
       LabelSize.Text = string.Format("Size: {0} MB", ConvertBytesToMegabytes(res.Result.Length));
       ImageDonwloaded.SetImageBitmap (imgBitmap);
      });
     }
     if(showHUD)
      AndHUD.Shared.Dismiss(this);
    });
   };

2. Async/Await method
AsyncButton.Click += async (sender, e) => {
    showHUD = ckbHUD.Checked;
    if(showHUD)
     AndHUD.Shared.Show(this, "Downloading Image via Async/Await", -1, MaskType.Clear);
    var httpClient = new HttpClient();
    byte[] imageBytes  = await httpClient.GetByteArrayAsync("http://upload.wikimedia.org/wikipedia/commons/6/66/Big_size_chess_6759_CRI_08_2009_Langosta_Beach.jpg");
    string documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    string localPath = System.IO.Path.Combine (documents, "image.png");
    File.WriteAllBytes (localPath, imageBytes);
    var localImage = new Java.IO.File (localPath);
    if (localImage.Exists ()) {
     var imgBitmap = BitmapFactory.DecodeFile (localImage.AbsolutePath);
     LabelSize.Text = string.Format("Size: {0} MB", ConvertBytesToMegabytes(imageBytes.Length));
     ImageDonwloaded.SetImageBitmap (imgBitmap);
    }
    if(showHUD)
     AndHUD.Shared.Dismiss(this);
   };

If you see in the example we download a picture and then we save it to the app folder and after this we put the image into a imageview.

Note: when you use task a task(parallel) if  your expect to affect and ui element you need to implement the RunOnUiThread(()=>{});

Full Exmple at Github: https://github.com/AlejandroRuiz/Mono/tree/master/Responsive

remember if you have a question feel free to ask me, Happy Code  #CSHARPLOVERS


Comentarios

  1. Hi Author just now i found your blog its really awesome. Keep this work. It will more helpful for xamarin app developers.
    Hire affordable Xamarin Developer

    ResponderEliminar

Publicar un comentario

Entradas populares de este blog

Xamarin Month: Fall in Love with Xamarin.Forms Shell

ASP.NET Core Identity with Cosmos DB (MongoDB) Part2: ASP.NET Core Code

C# Interface Ejemplo Sencillo