SaveConfigViewModel.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 SaveConfigViewModel: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 SaveConfigViewModel()
  23. {
  24. Content = typeof(Views.SaveConfigView);
  25. }
  26. static SaveConfigViewModel()
  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.SaveFilePickerAsync(new Avalonia.Platform.Storage.FilePickerSaveOptions()
  45. {
  46. Title = p == null ? "" : App.Current?.FindResource(p) + "",
  47. FileTypeChoices = new FilePickerFileType[]
  48. {
  49. new FilePickerFileType($"{(p ==null ?"": App.Current?.FindResource(p) + "")}"){ Patterns = new []{"*.cfg" } }
  50. }
  51. });
  52. if (folder != null)
  53. {
  54. SelectedFile = folder.Path.LocalPath;
  55. }
  56. }
  57. }
  58. public static SaveConfigViewModel Instance { get; } = new SaveConfigViewModel();
  59. }
  60. }