private void SaveToFile(object param) { var path = param as string; try { ProjectPath = path ?? throw new Exception("null path"); SelectedMacro.EditingMode = false; SelectedScenario.EditingMode = false; SelectedTemplate.EditingMode = false; ViewModelArgs args = ViewModelArgs.CreateFromViewModel(this); BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) { formatter.Serialize(stream, args); } } catch (Exception e) { Logger.GetLogger().CatchException("ViewModel", "SaveToFile", e.Message); throw; } #if DEBUGLOG Logger.GetLogger().WriteToLog($"ViewModel: SaveToFile: Path{{{path}}} : Code{{{1}}}"); #endif }
//Factory public static ViewModelArgs CreateFromViewModel(ViewModel viewModel) { ViewModelArgs args = new ViewModelArgs { MacroList = viewModel.MacroList, Scenarios = viewModel.Scenarios, ActionTemplates = viewModel.ActionTemplates, SelectedMacro = viewModel.SelectedMacro, SelectedScenario = viewModel.SelectedScenario }; return(args); }
private void LoadFromFile(object param) { var path = param as string; try { ProjectPath = path ?? throw new Exception("null path"); SelectedTemplate.EditingMode = false; SelectedTemplate = null; BinaryFormatter formatter = new BinaryFormatter(); WinWrapper.UnregisterAll(); using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate)) { ViewModelArgs args = (ViewModelArgs)formatter.Deserialize(stream); MacroList = args.MacroList; Scenarios = args.Scenarios; foreach (var action in Scenarios) { WinWrapper.RegisterKey(action.HotKey); } ActionTemplates = args.ActionTemplates; foreach (var action in ActionTemplates) { WinWrapper.RegisterKey(action.HotKey); } SelectedMacro = args.SelectedMacro; SelectedScenario = args.SelectedScenario; } } catch (Exception e) { Logger.GetLogger().CatchException("ViewModel", "LoadFromFile", e.Message); throw; } #if DEBUGLOG Logger.GetLogger().WriteToLog($"ViewModel: LoadFromFile: Path{{{path}}} : Code{{{1}}}"); #endif }