Exemplo n.º 1
0
        public override async Task ExecuteAsync()
        {
            if (_docInfoService.IsOpenedDocument)
            {
                await _textFileWriter.WriteAsync(new WriteTextFileModel { FilePath = _docInfoService.UsedFilePath, Content = CallerViewModel.InputTextBoxViewModel.Content });

                _docInfoService.SetUnmodifiedDocumentState();

                CallerViewModel.WindowSettingsViewModel.Title = _docInfoService.UsedFileNameWithoutExtension;
            }
            else
            {
                SaveFileDialogResult saveFileDialogResult =
                    await _saveFileDialog.ShowDialogAsync(new SaveFileDialogOptions { FileFilters = GetSaveFileDialogFilters() });

                if (saveFileDialogResult.SaveFileDialogResultType == SaveFileDialogResultType.Ok)
                {
                    await _textFileWriter.WriteAsync(new WriteTextFileModel { FilePath = saveFileDialogResult.SavedFilePath, Content = CallerViewModel.InputTextBoxViewModel.Content });

                    _docInfoService.SetFilePath(saveFileDialogResult.SavedFilePath);
                    _docInfoService.SetUnmodifiedDocumentState();

                    CallerViewModel.WindowSettingsViewModel.Title = _docInfoService.UsedFileNameWithoutExtension;
                }
            }
        }
        public override async Task ExecuteAsync()
        {
            if (_docInfoService.IsModifiedDocument)
            {
                MessageDialogResult messageDialogResult = await _messageDialog.ShowDialogAsync(
                    new MessageDialogOptions { Title = "Notepad", Content = $"Do you want to save the {_docInfoService.UsedFileNameWithExtension} changes?", Button = MessageBoxButton.YesNoCancel });

                switch (messageDialogResult.MessageDialogResultType)
                {
                case MessageDialogResultType.Yes:
                {
                    if (_docInfoService.IsOpenedDocument)
                    {
                        await _textFileWriter.WriteAsync(new WriteTextFileModel { FilePath = _docInfoService.UsedFilePath, Content = CallerViewModel.InputTextBoxViewModel.Content });
                    }
                    else
                    {
                        SaveFileDialogResult saveFileDialogResult = await _saveFileDialog.ShowDialogAsync(new SaveFileDialogOptions { FileFilters = GetSaveFileDialogFilters() });

                        if (saveFileDialogResult.SaveFileDialogResultType == SaveFileDialogResultType.Ok)
                        {
                            await _textFileWriter.WriteAsync(new WriteTextFileModel { FilePath = saveFileDialogResult.SavedFilePath, Content = CallerViewModel.InputTextBoxViewModel.Content });

                            _docInfoService.SetFilePath(saveFileDialogResult.SavedFilePath);
                        }
                        else if (saveFileDialogResult.SaveFileDialogResultType == SaveFileDialogResultType.Cancel)
                        {
                            return;
                        }
                    }

                    _docInfoService.SetUnmodifiedDocumentState();
                    CallerViewModel.WindowSettingsViewModel.Title = _docInfoService.UsedFileNameWithoutExtension;

                    break;
                }

                case MessageDialogResultType.Cancel:
                {
                    return;
                }
                }
            }

            OpenFileDialogResult openFileDialogResult = await _openFileDialog.ShowDialogAsync(new OpenFileDialogOptions { FileFilters = GetOpenFileDialogFilters() });

            if (openFileDialogResult.OpenFileDialogResultType == OpenFileDialogResultType.Ok)
            {
                CallerViewModel.InputTextBoxViewModel.Content = await _textFileReader.ReadAsync <string>(new ReadTextFileModel { FilePath = openFileDialogResult.FilePath });

                _docInfoService.SetFilePath(openFileDialogResult.FilePath);
                _docInfoService.SetUnmodifiedDocumentState();

                CallerViewModel.WindowSettingsViewModel.Title = _docInfoService.UsedFileNameWithoutExtension;
            }
        }
        public override async Task ExecuteAsync(CancelEventArgs eventArgs)
        {
            if (_docInfoService.IsModifiedDocument)
            {
                MessageDialogResult messageDialogResult = await _messageDialog.ShowDialogAsync(
                    new MessageDialogOptions { Title = "Notepad", Content = $"Do you want to save the {_docInfoService.UsedFileNameWithExtension} changes?", Button = MessageBoxButton.YesNoCancel });

                switch (messageDialogResult.MessageDialogResultType)
                {
                case MessageDialogResultType.Yes:
                {
                    if (_docInfoService.IsOpenedDocument)
                    {
                        await _textFileWriter.WriteAsync(new WriteTextFileModel { FilePath = _docInfoService.UsedFilePath, Content = CallerViewModel.InputTextBoxViewModel.Content });
                    }
                    else
                    {
                        SaveFileDialogResult saveFileDialogResult = await _saveFileDialog.ShowDialogAsync(new SaveFileDialogOptions { FileFilters = GetSaveFileDialogFilters() });

                        if (saveFileDialogResult.SaveFileDialogResultType == SaveFileDialogResultType.Ok)
                        {
                            await _textFileWriter.WriteAsync(new WriteTextFileModel { FilePath = saveFileDialogResult.SavedFilePath, Content = CallerViewModel.InputTextBoxViewModel.Content });
                        }
                        else if (saveFileDialogResult.SaveFileDialogResultType == SaveFileDialogResultType.Cancel)
                        {
                            eventArgs.Cancel = true;
                            return;
                        }
                    }

                    break;
                }

                case MessageDialogResultType.Cancel:
                {
                    eventArgs.Cancel = true;
                    return;
                }
                }
            }

            System.Windows.Application.Current.Shutdown();
        }