LoadConfigViewModel.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Platform.Storage;
  5. using CommunityToolkit.Mvvm.Input;
  6. using Shaker.Models;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Input;
  13. namespace ShakerApp.ViewModels
  14. {
  15. internal class LoadConfigViewModel: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 LoadConfigViewModel()
  23. {
  24. Content = typeof(Views.LoadConfigView);
  25. }
  26. static LoadConfigViewModel()
  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 void Save()
  36. {
  37. base.Save();
  38. }
  39. public ICommand SelectFileCommand => new RelayCommand<string?>(SelectFile);
  40. private async void SelectFile(string? p)
  41. {
  42. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  43. {
  44. var folder = await TopLevel.GetTopLevel(desktop.MainWindow)!.StorageProvider.OpenFilePickerAsync(new Avalonia.Platform.Storage.FilePickerOpenOptions()
  45. {
  46. AllowMultiple = false,
  47. Title = p == null ? "" : App.Current?.FindResource(p) + "",
  48. FileTypeFilter = new FilePickerFileType[]
  49. {
  50. new FilePickerFileType($"{(p ==null ?"": App.Current?.FindResource(p) + "")}"){ Patterns = new []{"*.cfg" } }
  51. }
  52. });
  53. if (folder != null && folder.Count>0)
  54. {
  55. SelectedFile = folder.First().Path.LocalPath;
  56. }
  57. }
  58. }
  59. public static LoadConfigViewModel Instance { get; } = new LoadConfigViewModel();
  60. }
  61. }