LoginView.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using CommunityToolkit.Mvvm.Messaging;
  2. using Terminal.Gui.App;
  3. using Terminal.Gui.ViewBase;
  4. namespace CommunityToolkitExample;
  5. internal partial class LoginView : IRecipient<Message<LoginActions>>
  6. {
  7. public LoginView (LoginViewModel viewModel)
  8. {
  9. WeakReferenceMessenger.Default.Register (this);
  10. Title = $"Community Toolkit MVVM Example - {Application.QuitKey} to Exit";
  11. ViewModel = viewModel;
  12. InitializeComponent ();
  13. usernameInput.TextChanged += (_, _) =>
  14. {
  15. ViewModel.Username = usernameInput.Text;
  16. };
  17. passwordInput.TextChanged += (_, _) =>
  18. {
  19. ViewModel.Password = passwordInput.Text;
  20. };
  21. loginButton.Accepting += (_, e) =>
  22. {
  23. if (!ViewModel.CanLogin) { return; }
  24. ViewModel.LoginCommand.Execute (null);
  25. // When Accepting is handled, set e.Handled to true to prevent further processing.
  26. e.Handled = true;
  27. };
  28. clearButton.Accepting += (_, e) =>
  29. {
  30. ViewModel.ClearCommand.Execute (null);
  31. // When Accepting is handled, set e.Handled to true to prevent further processing.
  32. e.Handled = true;
  33. };
  34. Initialized += (_, _) => { ViewModel.Initialized (); };
  35. }
  36. public LoginViewModel ViewModel { get; set; }
  37. public void Receive (Message<LoginActions> message)
  38. {
  39. switch (message.Value)
  40. {
  41. case LoginActions.Clear:
  42. {
  43. loginProgressLabel.Text = ViewModel.LoginProgressMessage;
  44. validationLabel.Text = ViewModel.ValidationMessage;
  45. validationLabel.SetScheme (ViewModel.ValidationScheme);
  46. break;
  47. }
  48. case LoginActions.LoginProgress:
  49. {
  50. loginProgressLabel.Text = ViewModel.LoginProgressMessage;
  51. break;
  52. }
  53. case LoginActions.Validation:
  54. {
  55. validationLabel.Text = ViewModel.ValidationMessage;
  56. validationLabel.SetScheme (ViewModel.ValidationScheme);
  57. break;
  58. }
  59. }
  60. SetText ();
  61. // BUGBUG: This should not be needed:
  62. Application.LayoutAndDraw ();
  63. }
  64. private void SetText ()
  65. {
  66. usernameInput.Text = ViewModel.Username;
  67. usernameLengthLabel.Text = ViewModel.UsernameLengthMessage;
  68. passwordInput.Text = ViewModel.Password;
  69. passwordLengthLabel.Text = ViewModel.PasswordLengthMessage;
  70. }
  71. }