SelectTestDataViewModel.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Avalonia.Controls.ApplicationLifetimes;
  2. using Avalonia.Controls;
  3. using Avalonia.Platform.Storage;
  4. using CommunityToolkit.Mvvm.Input;
  5. using Shaker.Models;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Input;
  12. using Avalonia;
  13. namespace ShakerApp.ViewModels
  14. {
  15. internal class SelectTestDataViewModel:DisplayViewModelBase<IModel>
  16. {
  17. public override string OKContent => "Confirm";
  18. public override bool CanResize => false;
  19. public override double Height => 320;
  20. public override double Width => 620;
  21. private string selectedFile = string.Empty;
  22. private SelectTestDataViewModel()
  23. {
  24. Content = typeof(Views.LoadTestDataView);
  25. }
  26. static SelectTestDataViewModel()
  27. {
  28. }
  29. public string SelectedFile { get => selectedFile; set => SetProperty(ref selectedFile, value); }
  30. public override void InitData()
  31. {
  32. base.InitData();
  33. SelectedFile = string.Empty;
  34. }
  35. protected override async void Save()
  36. {
  37. base.Save();
  38. if(!string.IsNullOrEmpty(SelectedFile) && System.IO.File.Exists(SelectedFile))
  39. {
  40. await Task.Delay(1);
  41. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  42. {
  43. DataReviewViewModel.Instance.InitData();
  44. DataReviewViewModel.Instance.Title = "DataReview";
  45. BaseDialogWindow window = new BaseDialogWindow();
  46. window.DataContext = DataReviewViewModel.Instance;
  47. DeviceMangerViewModel.Instance.CloseWindowAction = () => window?.Close();
  48. DataReviewViewModel.Instance.OpenFile(SelectedFile);
  49. await window.ShowDialog(desktop.MainWindow!);
  50. }
  51. }
  52. }
  53. public ICommand SelectFileCommand => new RelayCommand<string?>(SelectFile);
  54. private async void SelectFile(string? p)
  55. {
  56. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  57. {
  58. var folder = await TopLevel.GetTopLevel(desktop.MainWindow)!.StorageProvider.OpenFilePickerAsync(new Avalonia.Platform.Storage.FilePickerOpenOptions()
  59. {
  60. AllowMultiple = false,
  61. Title = p == null ? "" : App.Current?.FindResource(p) + "",
  62. FileTypeFilter = new FilePickerFileType[]
  63. {
  64. new FilePickerFileType($"{(p ==null ?"": App.Current?.FindResource(p) + "")}"){ Patterns = new []{"*.tdms" } }
  65. }
  66. });
  67. if (folder != null && folder.Count > 0)
  68. {
  69. SelectedFile = folder.First().Path.LocalPath;
  70. }
  71. }
  72. }
  73. public static SelectTestDataViewModel Instance { get; } = new SelectTestDataViewModel();
  74. }
  75. }