MainWindowViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Avalonia.Controls;
  2. using ReactiveUI;
  3. using System.Threading;
  4. namespace Captchapp.ViewModels
  5. {
  6. public class MainWindowViewModel : ViewModelBase
  7. {
  8. UserControl control = new Page1();
  9. public UserControl Control {
  10. get => control;
  11. set => this.RaiseAndSetIfChanged(ref control, value);
  12. }
  13. Page1ViewModel page1VM = new Page1ViewModel();
  14. Page2ViewModel page2VM = new Page2ViewModel();
  15. public Page1ViewModel Page1VM {
  16. get => page1VM;
  17. set => page1VM = value;
  18. }
  19. public Page2ViewModel Page2VM
  20. {
  21. get => page2VM;
  22. set => page2VM = value;
  23. }
  24. public void toPage1()
  25. {
  26. Control = new Page1();
  27. }
  28. public void toPage2()
  29. {
  30. if (Page1VM.Authorize())
  31. {
  32. Control = new Page2();
  33. Page1VM.ErrorMessage = "";
  34. Page1VM.ErrorVisibility = false;
  35. }
  36. else
  37. {
  38. Page1VM.ErrorMessage = "Логин или пароль неправильные. Повторите попытку";
  39. Page1VM.ErrorVisibility = true;
  40. Page1VM.makeCaptcha();
  41. Page1VM.LoginVisibility = false;
  42. }
  43. }
  44. private Timer timer;
  45. private int count = 0;
  46. public void toPage2Captcha()
  47. {
  48. if (Page1VM.checkCaptcha())
  49. {
  50. Page1VM.ErrorVisibility = false;
  51. Page1VM.LoginVisibility = true;
  52. Page1VM.CaptchaVisibility = false;
  53. Control = new Page2();
  54. Page1VM.ErrorMessage = "";
  55. }
  56. else
  57. {
  58. Page1VM.ErrorMessage = "Логин или пароль неправильные. Повторите попытку";
  59. Page1VM.ErrorVisibility = true;
  60. Page1VM.makeCaptcha();
  61. Page1VM.CaptchaVisibility = false;
  62. Page1VM.LoginVisibility = false;
  63. // Здесь вызывается таймер
  64. Page1VM.TimerVisibility = true;
  65. Page1VM.TextBoxEnable = false;
  66. Page1VM.Password = "";
  67. Page1VM.Email = "";
  68. timer = new Timer(timerText, null, 0,1000);
  69. }
  70. }
  71. public void timerText(object? obj)
  72. {
  73. count++;
  74. Page1VM.TimerMessage = $"Попробуйте снова через {10-count} секунд";
  75. if (count >= 10) {
  76. Page1VM.TimerVisibility = false;
  77. Page1VM.TextBoxEnable = true;
  78. Page1VM.CaptchaVisibility = true;
  79. count = 0;
  80. timer.Dispose();
  81. }
  82. }
  83. }
  84. }