private async void SaveFileDialogButtonClickHandler(object sender, RoutedEventArgs args) { SaveFileDialogArguments dialogArgs = new SaveFileDialogArguments() { Width = 600, Height = 400, Filters = "All files|*.*|C# files|*.cs|XAML files|*.xaml" }; SaveFileDialogResult result = await SaveFileDialog.ShowDialogAsync(MainWindow.DialogHostName, dialogArgs); if (DataContext is FileSystemDialogViewModel viewModel) { if (!result.Canceled) { viewModel.SelectedAction = "Selected file: " + result.FileInfo.FullName; } else { viewModel.SelectedAction = "Cancel save file"; } } }
public MainWindowViewModel() { Dictionary <string, Page> pageCache = new Dictionary <string, Page>(); Model = new MainWindowModel(); Page = Model.ToReactivePropertySlimAsSynchronized(m => m.Page); TransitPage = new ReactiveCommand <string>(); TransitPage.Subscribe(page => { if (!pageCache.ContainsKey(page)) { pageCache.Add(page, (Page)Activator.CreateInstance(null !, page)?.Unwrap() !); } Page.Value = pageCache[page] !; }); ButtonPower = new AsyncReactiveCommand(); ButtonPower.Subscribe(async _ => { var vm = new ConfirmDialogViewModel { Message = { Value = "Are you sure to quit the application?" } }; var dialog = new ConfirmDialog { DataContext = vm }; var res = await DialogHost.Show(dialog, "MessageDialogHost"); if (res is bool quit && quit) { AUTDHandler.Instance.Dispose(); SettingManager.SaveSetting("settings.json"); Application.Current.Shutdown(); } }); OpenUrl = new ReactiveCommand <string>(); OpenUrl.Subscribe(url => { var psi = new ProcessStartInfo { FileName = url, UseShellExecute = true }; Process.Start(psi); }); Save = new AsyncReactiveCommand(); Save.Subscribe(async _ => { var dialogArgs = new SaveFileDialogArguments { Width = 600, Height = 800, Filters = "json files|*.json", ForceFileExtensionOfFileFilter = true, CreateNewDirectoryEnabled = true }; var result = await SaveFileDialog.ShowDialogAsync("MessageDialogHost", dialogArgs); if (result.Canceled) { return; } try { SettingManager.SaveSetting(result.File); } catch { var vm = new ErrorDialogViewModel { Message = { Value = "Failed to save settings." } }; var dialog = new ErrorDialog { DataContext = vm }; await DialogHost.Show(dialog, "MessageDialogHost"); } }); Load = new AsyncReactiveCommand(); Load.Subscribe(async _ => { var dialogArgs = new OpenFileDialogArguments { Width = 600, Height = 800, Filters = "json files|*.json" }; var result = await OpenFileDialog.ShowDialogAsync("MessageDialogHost", dialogArgs); if (result.Canceled) { return; } try { SettingManager.LoadSetting(result.File); } catch { var vm = new ErrorDialogViewModel { Message = { Value = "Failed to load settings." } }; var dialog = new ErrorDialog { DataContext = vm }; await DialogHost.Show(dialog, "MessageDialogHost"); } }); Start = AUTDHandler.Instance.IsRunning.Select(x => !x).ToAsyncReactiveCommand(); Start.Subscribe(async _ => { if (!AUTDHandler.Instance.IsOpen.Value) { var res = await Task.Run(() => AUTDHandler.Instance.Open()); if (res != null) { var vm = new ErrorDialogViewModel { Message = { Value = $"Failed to open AUTD: {res}.\nSee Link options." } }; var dialog = new ErrorDialog { DataContext = vm }; await DialogHost.Show(dialog, "MessageDialogHost"); return; } } AUTDHandler.Instance.SendGain(); AUTDHandler.Instance.SendModulation(); }); Stop = AUTDHandler.Instance.IsRunning.Select(x => x).ToReactiveCommand(); Stop.Subscribe(_ => { AUTDHandler.Instance.Stop(); }); }