ContextMenuPageViewModel.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. using System.Reactive;
  3. using System.Threading.Tasks;
  4. using Avalonia.Controls;
  5. using ReactiveUI;
  6. namespace ControlCatalog.ViewModels
  7. {
  8. public class ContextMenuPageViewModel
  9. {
  10. public ContextMenuPageViewModel()
  11. {
  12. OpenCommand = ReactiveCommand.CreateFromTask(Open);
  13. SaveCommand = ReactiveCommand.Create(Save);
  14. OpenRecentCommand = ReactiveCommand.Create<string>(OpenRecent);
  15. MenuItems = new[]
  16. {
  17. new MenuItemViewModel { Header = "_Open...", Command = OpenCommand },
  18. new MenuItemViewModel { Header = "Save", Command = SaveCommand },
  19. new MenuItemViewModel { Header = "-" },
  20. new MenuItemViewModel
  21. {
  22. Header = "Recent",
  23. Items = new[]
  24. {
  25. new MenuItemViewModel
  26. {
  27. Header = "File1.txt",
  28. Command = OpenRecentCommand,
  29. CommandParameter = @"c:\foo\File1.txt"
  30. },
  31. new MenuItemViewModel
  32. {
  33. Header = "File2.txt",
  34. Command = OpenRecentCommand,
  35. CommandParameter = @"c:\foo\File2.txt"
  36. },
  37. }
  38. },
  39. };
  40. }
  41. public IReadOnlyList<MenuItemViewModel> MenuItems { get; set; }
  42. public ReactiveCommand<Unit, Unit> OpenCommand { get; }
  43. public ReactiveCommand<Unit, Unit> SaveCommand { get; }
  44. public ReactiveCommand<string, Unit> OpenRecentCommand { get; }
  45. public async Task Open()
  46. {
  47. var dialog = new OpenFileDialog();
  48. var result = await dialog.ShowAsync(App.Current.MainWindow);
  49. if (result != null)
  50. {
  51. foreach (var path in result)
  52. {
  53. System.Diagnostics.Debug.WriteLine($"Opened: {path}");
  54. }
  55. }
  56. }
  57. public void Save()
  58. {
  59. System.Diagnostics.Debug.WriteLine("Save");
  60. }
  61. public void OpenRecent(string path)
  62. {
  63. System.Diagnostics.Debug.WriteLine($"Open recent: {path}");
  64. }
  65. }
  66. }