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