Beispiel #1
0
        public void CreateTileForBook(string bookId)
        {
            if (string.IsNullOrWhiteSpace(bookId))
            {
                throw new ArgumentException("bookId is invalid");
            }

            var book = _bookRepository.Get(bookId);

            var uri = _navigationService.UriFor <ReadPageViewModel>()
                      .WithParam(vm => vm.BookId, bookId)
                      .WithParam(vm => vm.ToLastReadPage, true)
                      .BuildUri();


            string      title = book.Title;
            BitmapImage bmp;

            using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ModelExtensions.GetBookCoverPath(book.BookID)))
                {
                    bmp = new BitmapImage()
                    {
                        CreateOptions = BitmapCreateOptions.None
                    };
                    using (var file = storage.OpenFile(ModelExtensions.GetBookCoverPath(book.BookID), FileMode.Open))
                    {
                        bmp.SetSource(file);
                        title = string.Empty;
                    }
                }
                else
                {
                    bmp = null;//.UriSource = new Uri("/Resources/Icons/tile.png", UriKind.RelativeOrAbsolute);
                }
            }

            BookTile tile = new BookTile();

            tile.ImageSource = bmp;
            tile.Title       = title;
            tile.UpdateLayout();

            LiveTileHelper.CreateOrUpdateTile(new RadExtendedTileData()
            {
                VisualElement = tile
            }, uri);
        }
 private static void DeleteFolder(string bookId)
 {
     try
     {
         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
         {
             string path = ModelExtensions.GetBookCoverPath(bookId);
             if (file.FileExists(path))
             {
                 file.DeleteFile(path);
             }
             string coverPath = ModelExtensions.GetBookFullCoverPath(bookId);
             if (file.FileExists(coverPath))
             {
                 file.DeleteFile(coverPath);
             }
             if (file.DirectoryExists(bookId))
             {
                 string[] fileNames = file.GetFileNames(bookId + @"\*.*");
                 bool     flag      = false;
                 foreach (string fileName in fileNames)
                 {
                     try
                     {
                         file.DeleteFile(Path.Combine(bookId, fileName));
                     }
                     catch (Exception)
                     {
                         flag = true;
                     }
                 }
                 if (!flag)
                 {
                     file.DeleteDirectory(bookId);
                 }
             }
         }
     }
     catch (Exception)
     {
     }
 }
        protected bool SaveCoverImages(string bookID, Stream imageStream)
        {
            var  @event = new AutoResetEvent(false);
            bool result = false;

            ((Action)(() =>
            {
                try
                {
                    var bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(imageStream);

                    using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var iconImageFile = isoStorage.CreateFile(ModelExtensions.GetBookCoverPath(bookID)))
                        {
                            bitmapImage.SaveJpeg(iconImageFile, 300, 300, true);
                        }

                        using (var coverImageFile = isoStorage.CreateFile(ModelExtensions.GetBookFullCoverPath(bookID)))
                        {
                            bitmapImage.SaveJpeg(coverImageFile, bitmapImage.PixelWidth, bitmapImage.PixelHeight, false);
                        }
                    }
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
                finally
                {
                    @event.Set();
                }
            })).OnUIThread();

            @event.WaitOne();
            return(result);
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return(null);
            }

            var bookModel = (BookModel)value;
            var fullCover = System.Convert.ToBoolean(parameter);

            var path = fullCover ? ModelExtensions.GetBookFullCoverPath(bookModel.BookID) : ModelExtensions.GetBookCoverPath(bookModel.BookID);

            var task = Task.Run(async() =>
            {
                var taskCompletionSource  = new TaskCompletionSource <BitmapImage>();
                MemoryStream memoryStream = null;

                using (var storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    try
                    {
                        if (!storeForApplication.FileExists(path))
                        {
                            throw new FileNotFoundException(path);
                        }
                        using (var storageFileStream = storeForApplication.OpenFile(path, FileMode.Open))
                        {
                            memoryStream = new MemoryStream((int)storageFileStream.Length);
                            storageFileStream.CopyTo(memoryStream, 4096);
                        }
                    }
                    catch (Exception)
                    {
                        taskCompletionSource.SetResult(null);
                    }
                }

                Execute.OnUIThread(() =>
                {
                    var bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(memoryStream);
                    taskCompletionSource.SetResult(bitmapImage);
                });

                return(await taskCompletionSource.Task);
            });

            return(new TaskCompletionNotifier <BitmapImage>(task));
        }