22using System . Reactive . Linq ;
33using ReactiveUI ;
44
5- namespace ClientSideExample . ViewModels
5+ namespace ClientSideExample . ViewModels ;
6+
7+ public class GreetingViewModel : ReactiveObject
68{
7- public class GreetingViewModel : ReactiveObject
8- {
9- private string _name ;
9+ private string _name ;
1010
11- private readonly ObservableAsPropertyHelper < bool > _canClear ;
12- private readonly ObservableAsPropertyHelper < string > _greeting ;
11+ private readonly ObservableAsPropertyHelper < bool > _canClear ;
12+ private readonly ObservableAsPropertyHelper < string > _greeting ;
1313
14- public ReactiveCommand < Unit , Unit > Clear { get ; }
14+ public ReactiveCommand < Unit , Unit > Clear { get ; }
1515
16- public bool CanClear => _canClear . Value ;
16+ public bool CanClear => _canClear . Value ;
1717
18- public string Greeting => _greeting . Value ;
19-
20- public string Name
21- {
22- get => _name ;
23- set => this . RaiseAndSetIfChanged ( ref _name , value ) ;
24- }
25-
26- public GreetingViewModel ( )
27- {
28- var canClear = this . WhenAnyValue ( x => x . Name )
29- . Select ( name => ! string . IsNullOrEmpty ( name ) ) ;
30-
31- Clear = ReactiveCommand . Create (
32- ( ) => { Name = string . Empty ; } ,
33- canClear ) ;
34-
35- _canClear = Clear . CanExecute
36- . ToProperty ( this , x => x . CanClear ) ;
37-
38- _greeting = this . WhenAnyValue ( x => x . Name )
39- . Select ( x => string . IsNullOrWhiteSpace ( x ) ? string . Empty : $ "Hello, { x } !")
40- . ToProperty ( this , x => x . Greeting ) ;
41- }
18+ public string Greeting => _greeting . Value ;
19+
20+ public string Name
21+ {
22+ get => _name ;
23+ set => this . RaiseAndSetIfChanged ( ref _name , value ) ;
24+ }
25+
26+ public GreetingViewModel ( )
27+ {
28+ var canClear = this . WhenAnyValue ( x => x . Name )
29+ . Select ( name => ! string . IsNullOrEmpty ( name ) ) ;
30+
31+ Clear = ReactiveCommand . Create (
32+ ( ) => { Name = string . Empty ; } ,
33+ canClear ) ;
34+
35+ _canClear = Clear . CanExecute
36+ . ToProperty ( this , x => x . CanClear ) ;
37+
38+ _greeting = this . WhenAnyValue ( x => x . Name )
39+ . Select ( x => string . IsNullOrWhiteSpace ( x ) ? string . Empty : $ "Hello, { x } !")
40+ . ToProperty ( this , x => x . Greeting ) ;
4241 }
43- }
42+ }
0 commit comments