title: Xamarin ProgressBar not updating real time
body: <p>All,</p>
  
  <p>I'm fairly new to Xamarin and more notably, C# so please, be kind ...!</p>
  
  <p>I have a Xamarin.Forms application that drops down to .iOS using a DependencyService which then traverses all of the songs on my device and returns using a custom "Song" model.</p>
  
  <p>There's no doubt a better way to do it but in order to return the album artwork back to the PCL, I've taken the iOS UIImage and turned it into a System.IO.Stream object and am returning that through the Song model.</p>
  
  <p>Adding this artwork functionality resulted in a much larger overhead when processing each song.  To try and give the user an appreciation for what is going on, I've put a progress bar on the page and want it to update each time I process a single song.</p>
  
  <p>My problem is, I've been unable to get the progress bar to update in real time.  It only updates once the process is complete.</p>
  
  <p>I'm not using MVVM at this stage, so this is the code behind ...</p>
  
  <pre><code>using Xamarin.Forms;
  using TestApplication.Interfaces;
  using System.Threading.Tasks;
  using System.ComponentModel;
  using System.Runtime.CompilerServices;
  using System;
  
  namespace TestApplication
  {
      public partial class TestApplicationPage : ContentPage, INotifyPropertyChanged
      {
          private double _progress;
          public double Progress
          {
              get { return _progress; }
              set
              {
                  _progress = value;
                  OnPropertyChanged();
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          public TestApplication()
          {
              InitializeComponent();
              BindingContext = this;
          }
  
          private void OnPropertyChanged([CallerMemberName] string propertyName = null)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
  
          async void GetNoOfSongs(object sender, System.EventArgs e)
          {
              Action&lt;double&gt; progressUpdate = UpdateProgress;
  
              var songs = await DependencyService.Get&lt;IMedia&gt;().GetSongs(progressUpdate);
  
              await DisplayAlert("No. of Songs", string.Format("You have { 0} songs on your device!", songs.Count), "Ok");
          }
  
          void UpdateProgress(double obj)
          {
              Progress = (double)obj;
          }
      }
  }
  </code></pre>
  
  <p>... this is the XAML page ...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestApplication" x:Class="TestApplication.TestApplicationPage"&gt;
      &lt;StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"&gt;
          &lt;Button Text="No. of Songs" Clicked="GetNoOfSongs"&gt;&lt;/Button&gt;
          &lt;ProgressBar Margin="20" x:Name="progressBar" Progress="{Binding Progress}" WidthRequest="400"&gt;&lt;/ProgressBar&gt;
      &lt;/StackLayout&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>This is the Song model ...</p>
  
  <pre><code>using System;
  using System.IO;
  
  namespace TestApplication.Models
  {
      public class Song
      {
          public string Artist { get; set; }
          public string Album { get; set; }
          public string Title { get; set; }
          public Stream Artwork { get; set; }
          public TimeSpan Duration { get; set; }
      }
  }
  </code></pre>
  
  <p>... this is the IMedia interface ...</p>
  
  <pre><code>using System.Threading.Tasks;
  using System.Collections.Generic;
  using TestApplication.Models;
  using System;
  
  namespace TestApplication.Interfaces
  {
      public interface IMedia
      {
          Task&lt;bool&gt; IsAuthorised();
          Task&lt;List&lt;Song&gt;&gt; GetSongs(Action&lt;double&gt; callback);
      }
  }
  </code></pre>
  
  <p>... and this is the DependencyService implementation within the .iOS project ...</p>
  
  <pre><code>using TestApplication.Interfaces;
  using TestApplication.Models;
  using MediaPlayer;
  using System;
  using System.Threading.Tasks;
  using System.Collections.Generic;
  using System.IO;
  
  [assembly: Xamarin.Forms.Dependency(typeof(TestApplication.iOS.Media))]
  namespace TestApplication.iOS
  {
      public class Media : IMedia
      {
          public List&lt;Song&gt; Songs { get; private set; }
  
          public async Task&lt;bool&gt; IsAuthorised()
          {
              await MPMediaLibrary.RequestAuthorizationAsync();
  
              if (MPMediaLibrary.AuthorizationStatus == MPMediaLibraryAuthorizationStatus.Authorized)
                  return true;
              else
                  return false;
          }
  
          public async Task&lt;List&lt;Song&gt;&gt; GetSongs(Action&lt;double&gt; callback)
          {
              Songs = new List&lt;Song&gt; { };
  
              if (await IsAuthorised())
              {
                  var songs = MPMediaQuery.SongsQuery.Items;
                  double index = 0;
  
                  foreach (var song in songs)
                  {
                      index++;
  
                      callback.Invoke(index / songs.Length);
  
                      Stream artwork = null;
  
                      if (song.Artwork != null)
                          artwork = song.Artwork.ImageWithSize(song.Artwork.Bounds.Size).AsPNG().AsStream();
  
                      Songs.Add(new Song
                      {
                          Artist = song.AlbumArtist,
                          Album = song.AlbumTitle,
                          Title = song.Title,
                          Artwork = artwork,
                          Duration = TimeSpan.FromSeconds(song.PlaybackDuration),
                      });
                  }
              }
  
              return Songs;
          }
      }
  }
  </code></pre>
  
  <p>... you'll see I've bound the progress value to a property.  Maybe for the purpose of this functionality I could just update it through the ProgressBar object when it runs but I know the binding works.</p>
  
  <p>I just can't put my finger on why it's not updating on the fly.  If I debug, it's going into the callback and updating the property and firing the OnPropertyChanged event but the UI isn't updating until the end.</p>
  
  <p>I'm thinking it has something to do with the whole async/await thing but can't be sure.</p>
  
  <p>I'm sure someone out there has the answer for me and I'm appreciative of any forthcoming help.</p>
  
  <p>Thanks</p>