Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public async void Open()
        {
            // prompt a picker
            var picker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.List,
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            };

            picker.FileTypeFilter.Add(".txt");

            var file = await picker.PickSingleFileAsync();

            if (file == null)
            {
                //await new Windows.UI.Popups.MessageDialog("No file selected.").ShowAsync();
                _ToastService.ShowToast("No file selected.");
            }
            else
            {
                File = await _FileService.LoadAsync(file);
            }
        }