Ejemplo n.º 1
0
        private async Task <bool> OpenSaveFileDialog(string fileContent, ExporButtonEventArgs.Formats fileFormats)
        {
            using (var saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                saveFileDialog.Filter           = fileFormats == ExporButtonEventArgs.Formats.Json
                    ? "(*.json)|*.json"
                    : "(*.xml)|*.xml";
                saveFileDialog.FilterIndex      = 1;
                saveFileDialog.RestoreDirectory = true;
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                if (string.IsNullOrEmpty(saveFileDialog.FileName))
                {
                    MessageBox.Show("File name cannot be empty", "Error", MessageBoxButtons.OK);
                    return(false);
                }

                var saved = await _productsViewModel.SaveFile(saveFileDialog.FileName, fileContent);

                return(saved);
            }
        }
Ejemplo n.º 2
0
        private async Task ExportToFile(ExporButtonEventArgs.Formats fileFormat)
        {
            var products = await _productsViewModel.GetProductsList();

            if (!products.Any())
            {
                return;
            }

            var exported = false;

            switch (fileFormat)
            {
            case ExporButtonEventArgs.Formats.Json:
                var jsonProducts = await products.ToJsonStringAsync();

                exported = await OpenSaveFileDialog(jsonProducts, fileFormat);

                break;

            case ExporButtonEventArgs.Formats.Xml:
                var xmlProducts = await products.ToList().ToXmlStringAsync();

                exported = await OpenSaveFileDialog(xmlProducts, fileFormat);

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(fileFormat), fileFormat, null);
            }

            if (exported)
            {
                MessageBox.Show($"File successfully exported!", "Success", MessageBoxButtons.OK);

                await _logger.LogInformation(
                    $"Successfully exported {fileFormat} file at {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            }
        }