title: UWP App with Grid View Losing Images when scrolling
body: <p>Rather new to UWP and XAML and I'm not sure how to debug this.</p>
  
  <p>I have a UWP app based on the Template10 Hamburger template and the Template10 incremental loading sample, plus some bits from a sample photo viewer (<a href="https://mtaulty.com/2013/01/" rel="nofollow">Windows 8: Making a Simple Photo Viewer in C# and XAML</a>).</p>
  
  <p>I have modified the main page to display a Gridview of images from the Pictures folder, with the images being loaded incrementally. I also pulled some from a sample photo viewer (<a href="https://mtaulty.com/2013/01/" rel="nofollow">Windows 8: Making a Simple Photo Viewer in C# and XAML</a>).</p>
  
  <p>When the app starts, the images are displayed as expected, and as I scroll down, the images are loaded and displayed on demand. The problem is as I scroll back up the list, the images are not displayed any more. My gridview items are still there showing the file name and coloured item background but the image is no longer drawn. </p>
  
  <p>To keep my memory footprint small, I am not storing the actual Bitmap image as part of my collection but a StorageItemThumbnail instead. I originally wanted to store just the image path but that doesn't work for anything in the Pictures library. </p>
  
  <pre><code>public class Picture
  {
      public StorageItemThumbnail  ImageThumbNail {get; set;}
      public string Name {get; set;}
  }
  </code></pre>
  
  <p>To display this, I use a converter class to create a stream to set the image source:</p>
  
  <pre><code>public object Convert(object value, Type targetType, object parameter, string language)
  {
  
      BitmapImage image = null;
  
      if (value != null)
      {
          if (value.GetType() != typeof(StorageItemThumbnail))
          {
              throw new ArgumentException("Expected a StorageItemThumbnail as binding input.");
          }
          if (targetType != typeof(ImageSource))
          {
              throw new ArgumentException("Expected ImageSource as binding output.");
          }
  
          if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
          {
              image = new BitmapImage();
              image.UriSource = new Uri("ms-appx:///Assets/DesignModeThumbnailPlaceholder.png");
          }
          else
          {
              StorageItemThumbnail ThumbNailFile = (StorageItemThumbnail)value;
  
              if (ThumbNailFile == null)
                  return image;
  
              image = new BitmapImage();
              IRandomAccessStream thumbnailStream = ThumbNailFile as IRandomAccessStream;
              image.SetSource(thumbnailStream);
          }
  
      }
  
      return (image);
  
  }
  </code></pre>
  
  <p>And this is bound in my XAML as follows:</p>
  
  <pre><code>    &lt;DataTemplate x:Name="PictureGridTemplate2"&gt;
      &lt;Grid Height="150" Width="150"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="*" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Border Grid.RowSpan="2" Background="Black" Opacity="0.8" /&gt;
          &lt;Image Width ="130" HorizontalAlignment="Center"
                 Stretch="Uniform" Source="{Binding ImageThumbNail, Converter={StaticResource ThumbnailFileToImageSourceConverter}}"/&gt;
          &lt;TextBlock Grid.Row="1" MaxHeight="30" Text="{Binding Name}"
                     Foreground="White" TextTrimming="CharacterEllipsis"/&gt;
      &lt;/Grid&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Can anyone point me in the direction where I've gone wrong with this?</p>
  
  <p>Sheri</p>
  
  <p><strong>* RESOLVED *</strong></p>
  
  <p>I was finally able to figure out what was causing this issue.</p>
  
  <p>It was a side effect of the gridview virtualization, my data model and how I was supplying the image through the converter.</p>
  
  <p>As a test I removed the converter, changed my data model to store a BitmapImage instance of the thumbnail (smaller than storing the whole image) and to bind directly to that property. This worked, the images displayed on the screen as I scrolled up and down through my gridview.</p>
  
  <p>I then changed my data model to have the BitmapImage property getter return the BitmapImage build on the fly from the StorageItemThumbnail property - same issue as when using the converter.</p>
  
  <p>By adding some debugging statements in the getter I saw that the height and width of the BitmapImage on the second request was 0. Aha! So why 0?</p>
  
  <p>Looking at the StorageItemThumbnail property on the second request I saw that the Stream position was at the EOF (not 0 like in the first request) - so this explained the 0 width and height, which explains the empty image control on the screen. </p>
  
  <p>I changed my code to use StorageItemThumbnail.CloneStream and now all images are displayed.</p>
  
  <p>Here is the converter method now:</p>
  
  <pre><code>    public object Convert(object value, Type targetType, object parameter, string language)
      {
  
          BitmapImage image = null;
  
          if (value != null)
          {
              if (value.GetType() != typeof(StorageItemThumbnail))
              {
                  throw new ArgumentException("Expected a StorageItemThumbnail as binding input.");
              }
              if (targetType != typeof(ImageSource))
              {
                  throw new ArgumentException("Expected ImageSource as binding output.");
              }
  
              if ((StorageItemThumbnail)value == null)
              {
                  System.Diagnostics.Debug.WriteLine("Thumbnail is null.");
                  return image;
              }
  
              image = new BitmapImage();
  
              using (var thumbNailClonedStream = ((StorageItemThumbnail)value).CloneStream())
              {
                  System.Diagnostics.Debug.WriteLine("Setting image source from cloned stream.");
                  image.SetSource(thumbNailClonedStream);
              } 
          }
  
          return (image);
  
      }
  </code></pre>
  
  <p>Thanks to everyone who took the time to answer and help point me in the right direction.</p>