/// <summary> /// Download the selected backup and replace the current data with it. /// </summary> /// <returns></returns> private async Task DownloadBackupAsync() { BackupIsIndeterminate = true; BackupStatusText = "Perpare download ..."; bool download = true; var dialog = new MessageDialog("Downloading a Backup will replace all local data.", "OneDrive Backup"); dialog.Commands.Add(new UICommand("Download", null, "Download")); dialog.Commands.Add(new UICommand("Cancel", null, "Cancel")); if ((await dialog.ShowAsync()).Id.ToString() == "Cancel") { download = false; } if (download) { var localBackup = await localStorageService.GetFileAsync(uploadZip); if (localBackup == null) { try { await zipArchiveService.CompressAsync(DataPath, uploadZip); // TODO: fix deprecation } catch { // TODO: Add error handling. } } BackupStatusText = $"Download Backup (total: {SelectedOneDriveItem.Size} bytes) ..."; try { var backupStream = await oneDriveService.GetContentByIdAsync(SelectedOneDriveItem.Id); if (backupStream != null) { // Delete folder await localStorageService.DeleteFolderAsync(DataPath); var folder = await localStorageService.CreateOrGetFileAsync(downloadZip); using (backupStream) { using (var fileStream = await folder.OpenStreamForWriteAsync()) { backupStream.Position = 0; fileStream.Position = 0; await backupStream.CopyToAsync(fileStream); } } try { await zipArchiveService.UncompressAsync(downloadZip); // TODO: fix deprecation } catch { await localStorageService.DeleteFolderAsync(DataPath); await zipArchiveService.UncompressAsync(uploadZip); // TODO: fix deprecation await new MessageDialog("An error occured while saving the backup. Old data have been restored.", "OneDrive Backup").ShowAsync(); } } else { await new MessageDialog("An error occured while downloading a backup. Please try again later.", "OneDrive Backup").ShowAsync(); } } catch (Exception) { await new MessageDialog("An error occured while downloading a backup. Please try again later.", "OneDrive Backup").ShowAsync(); } } await localStorageService.DeleteFileAsync(downloadZip); SelectedOneDriveItem = null; BackupIsIndeterminate = false; BackupStatusText = string.Empty; }
/// <summary> /// Creates the DiscoTile image and update the tile. /// </summary> /// <param name="inputMarkupFilename"></param> /// <param name="outputImageFilename"></param> /// <param name="size"></param> /// <returns></returns> private async Task CreateAndUpdateDiscoTileAsync(string inputMarkupFilename, string outputImageFilename, Size size) { // Get Tile markup var assetsFolder = await Package.Current.InstalledLocation.GetFolderAsync("Assets"); var markupStorageFile = await assetsFolder.GetFileAsync(inputMarkupFilename); // Update Elements var stream = await markupStorageFile.OpenReadAsync(); string markupContent; using (var sr = new StreamReader(stream.AsStreamForRead())) { markupContent = await sr.ReadToEndAsync(); } // Parse our custom tile XAML and create the object tree for this XAML Border border = (Border)XamlReader.Load(markupContent); // Create a new semi-transparent backgroud brush for the border, with a randomly chosen color. // This sample is setting a color for illustration purposes. // Transparency is also supported in tiles. // To make the tile transparent, so that the start screen background picture that your user selected shows through the // tile, set Background color property in the Package.appxmanifest to "transparent". border.Background = new SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255, 0, (byte)(random.Next() % 255), (byte)(random.Next() % 255))); // Change the text of the Title TextBlock. Grid grid = (Grid)border.Child; TextBlock titleText = (TextBlock)grid.FindName("Title"); titleText.Text = string.Format("{0:hh:mm}", DateTime.Now); // Set the source for the image that is displayed on the tile. Image logoImage = (Image)grid.FindName("LogoImage"); var bitmapImage = new BitmapImage() { CreateOptions = BitmapCreateOptions.None }; bitmapImage.UriSource = new Uri("ms-appx:///Assets/disco-ball.png"); logoImage.Source = bitmapImage; // Render DiscoTile RenderTargetBitmap rtb = new RenderTargetBitmap(); await rtb.RenderAsync(border, (int)size.Width, (int)size.Height); var buffer = await rtb.GetPixelsAsync(); // Read buffer into byte array. DataReader dataReader = DataReader.FromBuffer(buffer); var data = new byte[buffer.Length]; dataReader.ReadBytes(data); // Save DiscoTile to local storage var outputStorageFile = await localStorageService.CreateOrGetFileAsync(outputImageFilename); var outputStream = await outputStorageFile.OpenAsync(FileAccessMode.ReadWrite); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outputStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)size.Width, (uint)size.Height, 96, 96, data); await encoder.FlushAsync(); // Update Tile with image. UpdateTile(outputImageFilename); }