コード例 #1
0
        public async Task LoadAlbumsFromMediaLibrary(CancellationToken token)
        {
            using (var mediaLibrary = new MediaLibrary())
            {
                await Task.Delay(new TimeSpan(0, 0, 0, 0, 10));

                token.ThrowIfCancellationRequested();
                PictureAlbumCollection allAlbums = mediaLibrary.RootPictureAlbum.Albums;
                foreach (var album in allAlbums)
                {
                    token.ThrowIfCancellationRequested();
                    Items.Add(new CollectionControlModel()
                    {
                        FileName = Path.GetFileNameWithoutExtension(album.Name),
                        Data     = album
                    });

                    if (album.Pictures.Count == 0)
                    {
                        //set default thumbnail
                        continue;
                    }
                    token.ThrowIfCancellationRequested();
                    using (Stream str = album.Pictures.First().GetThumbnail())
                    {
                        byte[] buffer = new byte[str.Length];
                        await str.ReadAsync(buffer, 0, buffer.Length);

                        token.ThrowIfCancellationRequested();
                        MemoryStream ms = new MemoryStream();
                        await ms.WriteAsync(buffer, 0, buffer.Length);

                        ms.Seek(0, SeekOrigin.Begin);
                        token.ThrowIfCancellationRequested();
                        Items.Last().Thumbnail = new BitmapImage();
                        Items.Last().Thumbnail.SetSource(ms);
                    }
                }
            }
        }
コード例 #2
0
 private void Initialize()
 {
     ThreadPool.QueueUserWorkItem((WaitCallback)(o =>
     {
         try
         {
             Stopwatch stopwatch = Stopwatch.StartNew();
             List <AlbumHeader> albumHeaders = new List <AlbumHeader>();
             using (MediaLibrary mediaLibrary = new MediaLibrary())
             {
                 using (PictureAlbum rootPictureAlbum = mediaLibrary.RootPictureAlbum)
                 {
                     PictureAlbumCollection pictureAlbumCollection = rootPictureAlbum != null ? rootPictureAlbum.Albums : (PictureAlbumCollection)null;
                     if (pictureAlbumCollection != null)
                     {
                         if (pictureAlbumCollection.Count > 0)
                         {
                             using (IEnumerator <PictureAlbum> enumerator = pictureAlbumCollection.GetEnumerator())
                             {
                                 while (((IEnumerator)enumerator).MoveNext())
                                 {
                                     PictureAlbum current = enumerator.Current;
                                     if (current != null)
                                     {
                                         PictureCollection pictures = current.Pictures;
                                         if (pictures != null)
                                         {
                                             string str = current.Name ?? "";
                                             AlbumHeader albumHeader1 = new AlbumHeader();
                                             albumHeader1.AlbumId = str;
                                             albumHeader1.AlbumName = str;
                                             int count = pictures.Count;
                                             albumHeader1.PhotosCount = count;
                                             AlbumHeader albumHeader2 = albumHeader1;
                                             string albumName = albumHeader2.AlbumName;
                                             if (!(albumName == "Camera Roll"))
                                             {
                                                 if (!(albumName == "Saved Pictures"))
                                                 {
                                                     if (!(albumName == "Sample Pictures"))
                                                     {
                                                         if (albumName == "Screenshots")
                                                         {
                                                             albumHeader2.AlbumName = CommonResources.AlbumScreenshots;
                                                             albumHeader2.OrderNo = 3;
                                                         }
                                                         else
                                                         {
                                                             albumHeader2.OrderNo = int.MaxValue;
                                                         }
                                                     }
                                                     else
                                                     {
                                                         albumHeader2.AlbumName = CommonResources.AlbumSamplePictures;
                                                         albumHeader2.OrderNo = 2;
                                                     }
                                                 }
                                                 else
                                                 {
                                                     albumHeader2.AlbumName = CommonResources.AlbumSavedPictures;
                                                     albumHeader2.OrderNo = 1;
                                                 }
                                             }
                                             else
                                             {
                                                 albumHeader2.AlbumName = CommonResources.AlbumCameraRoll;
                                                 albumHeader2.OrderNo = 0;
                                             }
                                             Picture picture = ((IEnumerable <Picture>)pictures).FirstOrDefault <Picture>();
                                             if (picture != null)
                                             {
                                                 try
                                                 {
                                                     albumHeader2.ImageStream = picture.GetThumbnail();
                                                 }
                                                 catch
                                                 {
                                                 }
                                                 try
                                                 {
                                                     picture.Dispose();
                                                 }
                                                 catch
                                                 {
                                                 }
                                             }
                                             albumHeaders.Add(albumHeader2);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             stopwatch.Stop();
             Execute.ExecuteOnUIThread((Action)(() =>
             {
                 this._albums.Clear();
                 foreach (IEnumerable <AlbumHeader> source in albumHeaders.OrderBy <AlbumHeader, int>((Func <AlbumHeader, int>)(ah => ah.OrderNo)).Partition <AlbumHeader>(2))
                 {
                     List <AlbumHeader> list = source.ToList <AlbumHeader>();
                     AlbumHeaderTwoInARow albumHeaderTwoInArow = new AlbumHeaderTwoInARow()
                     {
                         AlbumHeader1 = list[0]
                     };
                     if (list.Count > 1)
                     {
                         albumHeaderTwoInArow.AlbumHeader2 = list[1];
                     }
                     this._albums.Add(albumHeaderTwoInArow);
                 }
                 this._isLoaded = true;
                 this.NotifyPropertyChanged <string>((Expression <Func <string> >)(() => this.FooterText));
                 this.NotifyPropertyChanged <Visibility>((Expression <Func <Visibility> >)(() => this.FooterTextVisibility));
             }));
         }
         catch (Exception ex)
         {
             Logger.Instance.ErrorAndSaveToIso("Failed to read gallery albums", ex);
         }
     }));
 }