public async void Save() { try { if (_file != null) { await _fileService.SaveAsync(_file); _toastService.ShowToast(_file, "File succesfully saved."); } else { var savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("Plain Text", new List <string>() { ".txt" }); // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = "New Document"; Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { // Prevent updates to the remote version of the file until // we finish making changes and call CompleteUpdatesAsync. Windows.Storage.CachedFileManager.DeferUpdates(file); // write to file await Windows.Storage.FileIO.WriteTextAsync(file, file.Name); // Let Windows know that we're finished changing the file so // the other app can update the remote version of the file. // Completing updates may require Windows to ask for user input. Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { await new Windows.UI.Popups.MessageDialog("File " + file.Name + " was saved.").ShowAsync(); } else { await new Windows.UI.Popups.MessageDialog("File " + file.Name + " couldn't be saved.").ShowAsync(); } } else { await new Windows.UI.Popups.MessageDialog("Operation cancelled.").ShowAsync(); } } } catch (Exception ex) { _toastService.ShowToast(_file, "Save failed. Error : " + ex.Message); } }
public async void Save() { //if this is a new file, (meaning no file was opened, then build the fileinfo model) if (File == null) { // get the current frame so that you can get the contents of the textbox. //Frame frame = (Frame)Window.Current.Content; //MainPage page = (MainPage)frame.Content; //TextBox textBox = (TextBox)page.FindName("textBox"); // set text value to contents of textbox or to an empty string. File = new Models.FileInfo { Text = textBox.Text ?? "" }; // use a save file picker to allow the user to save the file. GetPicker(File); } // not sure if this is a good idea. //I'm trying to force avoid saving in appdata folder. //But what about phones and cloud storage else if (File.Ref.Path.Contains("AppData")) { GetPicker(File); } else { try { // if the file existed, try and save. await _FileService.SaveAsync(File); // display a toast notification to let the user know that the file was saved. _ToastService.ShowToast(File, "File successfully saved."); } catch (Exception ex) { // if there was an error, display a toast notifying the user. _ToastService.ShowToast(File, $"Save failed: {ex.Message}"); } } }
Services.FileService _FileService = new Services.FileService(); // news-up a FileService public async void Save() // method that uses the service created above { await _FileService.SaveAsync(File); }