public async Task OpenFile()
        {
            // open file
            OpenFileDialogArguments dialogArgs = new OpenFileDialogArguments()
            {
                Width   = 600,
                Height  = 500,
                Filters = "All files (*.*)|*.*|Excel Worksheets (*.xls, *.xlsx)|*.xls;*.xlsx"
            };

            OpenFileDialogResult result = await OpenFileDialog.ShowDialogAsync(ShellDialogHostIdentifier.ToString(), dialogArgs);
        }
Пример #2
0
        private async void OpenFileDialogButtonClickHandler(object sender, RoutedEventArgs args)
        {
            OpenFileDialogArguments dialogArgs = new OpenFileDialogArguments()
            {
                Width  = 600,
                Height = 400
            };

            // await the result of OpenFileDialogResult and do something with it
            OpenFileDialogResult result = await OpenFileDialog.ShowDialogAsync(MainWindow.DialogHostName, dialogArgs);

            if (!result.Canceled)
            {
                Console.WriteLine("Selected file: " + result.FileInfo.FullName);
            }
            else
            {
                Console.WriteLine("Cancel open file");
            }
        }
Пример #3
0
        private async void OpenFileDialogButtonClickHandler(object sender, RoutedEventArgs args)
        {
            OpenFileDialogArguments dialogArgs = new OpenFileDialogArguments()
            {
                Width   = 600,
                Height  = 400,
                Filters = "All files|*.*|C# files|*.cs|XAML files|*.xaml"
            };

            OpenFileDialogResult result = await OpenFileDialog.ShowDialogAsync(MainWindow.DialogHostName, dialogArgs);

            if (DataContext is FileSystemDialogViewModel viewModel)
            {
                if (!result.Canceled)
                {
                    viewModel.SelectedAction = "Selected file: " + result.FileInfo.FullName;
                }
                else
                {
                    viewModel.SelectedAction = "Cancel open file";
                }
            }
        }
Пример #4
0
        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();
            });
        }