MainWindowViewModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Reactive;
  5. using System.Reactive.Linq;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8. using ReactiveUI;
  9. using Avalonia.Controls;
  10. namespace BindingDemo.ViewModels
  11. {
  12. public class MainWindowViewModel : ReactiveObject
  13. {
  14. private string _booleanString = "True";
  15. private double _doubleValue = 5.0;
  16. private string _stringValue = "Simple Binding";
  17. private bool _booleanFlag = false;
  18. private string _currentTime;
  19. private NestedCommandViewModel _nested;
  20. public MainWindowViewModel()
  21. {
  22. Items = new ObservableCollection<TestItem>(
  23. Enumerable.Range(0, 20).Select(x => new TestItem
  24. {
  25. StringValue = "Item " + x,
  26. Detail = "Item " + x + " details",
  27. }));
  28. Selection = new SelectionModel();
  29. ShuffleItems = ReactiveCommand.Create(() =>
  30. {
  31. var r = new Random();
  32. Items.Move(r.Next(Items.Count), 1);
  33. });
  34. StringValueCommand = ReactiveCommand.Create<object>(param =>
  35. {
  36. BooleanFlag = !BooleanFlag;
  37. StringValue = param.ToString();
  38. NestedModel = _nested ?? new NestedCommandViewModel();
  39. });
  40. Task.Run(() =>
  41. {
  42. while (true)
  43. {
  44. CurrentTime = DateTimeOffset.Now.ToString();
  45. Thread.Sleep(1000);
  46. }
  47. });
  48. CurrentTimeObservable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
  49. .Select(x => DateTimeOffset.Now);
  50. }
  51. public ObservableCollection<TestItem> Items { get; }
  52. public SelectionModel Selection { get; }
  53. public ReactiveCommand<Unit, Unit> ShuffleItems { get; }
  54. public string BooleanString
  55. {
  56. get { return _booleanString; }
  57. set { this.RaiseAndSetIfChanged(ref _booleanString, value); }
  58. }
  59. public double DoubleValue
  60. {
  61. get { return _doubleValue; }
  62. set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
  63. }
  64. public string StringValue
  65. {
  66. get { return _stringValue; }
  67. set { this.RaiseAndSetIfChanged(ref _stringValue, value); }
  68. }
  69. public bool BooleanFlag
  70. {
  71. get { return _booleanFlag; }
  72. set { this.RaiseAndSetIfChanged(ref _booleanFlag, value); }
  73. }
  74. public string CurrentTime
  75. {
  76. get { return _currentTime; }
  77. private set { this.RaiseAndSetIfChanged(ref _currentTime, value); }
  78. }
  79. public IObservable<DateTimeOffset> CurrentTimeObservable { get; }
  80. public ReactiveCommand<object, Unit> StringValueCommand { get; }
  81. public DataAnnotationsErrorViewModel DataAnnotationsValidation { get; } = new DataAnnotationsErrorViewModel();
  82. public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
  83. public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
  84. public NestedCommandViewModel NestedModel
  85. {
  86. get { return _nested; }
  87. private set { this.RaiseAndSetIfChanged(ref _nested, value); }
  88. }
  89. }
  90. }