private void Export(object item) { string name = string.Empty; if (item is XProject) { name = (item as XProject).Name; } else if (item is XDocument) { name = (item as XDocument).Name; } else if (item is XContainer) { name = (item as XContainer).Name; } var sb = new StringBuilder(); foreach (var writer in _projectEditor.FileWriters) { sb.Append($"{writer.Name} (*.{writer.Extension})|*.{writer.Extension}|"); } sb.Append("All (*.*)|*.*"); var dlg = new SaveFileDialog() { Filter = sb.ToString(), FilterIndex = 0, FileName = name }; if (dlg.ShowDialog(this) == true) { string result = dlg.FileName; IFileWriter writer = _projectEditor.FileWriters[dlg.FilterIndex - 1]; if (writer != null) { _projectEditor.OnExport(result, item, writer); } } }
/// <inheritdoc/> async Task IEditorApplication.OnExportAsync(object item) { string name = string.Empty; if (item is XContainer) { name = (item as XContainer).Name; } else if (item is XDocument) { name = (item as XDocument).Name; } else if (item is XProject) { name = (item as XProject).Name; } else if (item is ProjectEditor) { var editor = (item as ProjectEditor); if (editor?.Project == null) { return; } name = editor?.Project?.Name; item = editor?.Project; } else if (item == null) { if (_editor.Project == null) { return; } name = _editor?.Project?.Name; item = _editor?.Project; } var sb = new StringBuilder(); foreach (var writer in _editor?.FileWriters) { sb.Append($"{writer.Name} (*.{writer.Extension})|*.{writer.Extension}|"); } sb.Append("All (*.*)|*.*"); var dlg = new SaveFileDialog() { Filter = sb.ToString(), FilterIndex = 0, FileName = name }; if (dlg.ShowDialog(_mainWindow) == true) { string result = dlg.FileName; IFileWriter writer = _editor?.FileWriters[dlg.FilterIndex - 1]; if (writer != null) { _editor?.OnExport(result, item, writer); } } await Task.Delay(0); }
/// <inheritdoc/> async Task IEditorApplication.OnExportAsync(object item) { try { string name = string.Empty; if (item is XContainer) { name = (item as XContainer).Name; } else if (item is XDocument) { name = (item as XDocument).Name; } else if (item is XProject) { name = (item as XProject).Name; } else if (item is ProjectEditor) { var editor = (item as ProjectEditor); if (editor?.Project == null) { return; } name = editor?.Project?.Name; item = editor?.Project; } else if (item == null) { if (_editor?.Project == null) { return; } name = _editor?.Project?.Name; item = _editor?.Project; } var dlg = new SaveFileDialog(); foreach (var writer in _editor?.FileWriters) { dlg.Filters.Add(new FileDialogFilter() { Name = writer.Name, Extensions = { writer.Extension } }); } dlg.Filters.Add(new FileDialogFilter() { Name = "All", Extensions = { "*" } }); dlg.InitialFileName = name; dlg.DefaultExtension = _editor?.FileWriters.FirstOrDefault()?.Extension; var result = await dlg.ShowAsync(_mainWindow); if (result != null) { string ext = System.IO.Path.GetExtension(result).ToLower().TrimStart('.'); IFileWriter writer = _editor?.FileWriters.Where(w => string.Compare(w.Extension, ext, StringComparison.OrdinalIgnoreCase) == 0).FirstOrDefault(); if (writer != null) { _editor?.OnExport(result, item, writer); } } } catch (Exception ex) { _log?.LogError($"{ex.Message}{Environment.NewLine}{ex.StackTrace}"); } }