public static async Task <SaveModes> Save(ActorViewModel?actor, SaveModes mode = SaveModes.All) { if (actor == null) { return(mode); } SaveResult result = await FileService.Save <CharacterFile>(); if (string.IsNullOrEmpty(result.Path) || result.Info == null) { return(CharacterFileOptions.Result); } CharacterFile file = new CharacterFile(); file.WriteToFile(actor, mode); using FileStream stream = new FileStream(result.Path, FileMode.Create); result.Info.SerializeFile(file, stream); return(CharacterFileOptions.Result); }
public static async Task <SaveResult> Save <T>(string?path = null) where T : FileBase { SaveResult result = default; try { result.Info = GetFileInfo <T>(); if (path == null) { bool useExplorerBrowser = SettingsService.Current.UseWindowsExplorer; if (!useExplorerBrowser) { FileBrowserView browser = new FileBrowserView(result.Info, FileBrowserView.Modes.Save); await ViewService.ShowDrawer(browser); while (browser.IsOpen) { await Task.Delay(10); } path = browser.FilePath; useExplorerBrowser = browser.UseFileBrowser; result.Options = browser.OptionsControl; } if (useExplorerBrowser) { path = await App.Current.Dispatcher.InvokeAsync <string?>(() => { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = ToFilter(result.Info); bool?dlgResult = dlg.ShowDialog(); if (dlgResult != true) { return(null); } string?dirName = Path.GetDirectoryName(dlg.FileName); if (dirName == null) { throw new Exception("Failed to parse file name: " + dlg.FileName); } return(Path.Combine(dirName, Path.GetFileNameWithoutExtension(dlg.FileName))); }); } if (path == null) { return(result); } } path += "." + result.Info.Extension; result.Path = path; ////using FileStream stream = new FileStream(path, FileMode.Create); ////info.SerializeFile(file, stream); } catch (Exception ex) { Log.Error(ex, "Failed to save file"); } return(result); }