Closed SAgiKPJH closed 8 months ago
using DevExpress.Mvvm.CodeGenerators;
[GenerateViewModel]
partial class ViewModel {
[GenerateProperty]
string username;
[GenerateProperty]
string status;
[GenerateCommand]
void Login() => Status = "User: " + Username;
bool CanLogin() => !string.IsNullOrEmpty(Username);
}
대비되는 코드는 다음과 같습니다.
partial class ViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler? PropertyChanged;
protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
public string? Username {
get => username;
set {
if(EqualityComparer<string?>.Default.Equals(username, value)) return;
username = value;
RaisePropertyChanged(UsernameChangedEventArgs);
}
}
public string? Status {
get => status;
set {
if(EqualityComparer<string?>.Default.Equals(status, value)) return;
status = value;
RaisePropertyChanged(StatusChangedEventArgs);
}
}
DelegateCommand? loginCommand;
public DelegateCommand LoginCommand {
get => loginCommand ??= new DelegateCommand(Login, CanLogin, true);
}
static PropertyChangedEventArgs UsernameChangedEventArgs = new PropertyChangedEventArgs(nameof(Username));
static PropertyChangedEventArgs StatusChangedEventArgs = new PropertyChangedEventArgs(nameof(Status));
}
영상 제공 : https://github.com/DevExpress/DevExpress.Mvvm.CodeGenerators
[GenerateViewModel]
partial class ViewModel {
[GenerateProperty]
[StringLength(100, MinimumLength = 5)] // this point
string username;
}
Original
partial class ViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler? PropertyChanged;
protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
[System.ComponentModel.DataAnnotations.StringLengthAttribute(100, MinimumLength = 5)] // this point
public string? Username {
get => username;
set {
if(EqualityComparer<string?>.Default.Equals(username, value)) return;
username = value;
RaisePropertyChanged(UsernameChangedEventArgs);
}
}
static PropertyChangedEventArgs UsernameChangedEventArgs = new PropertyChangedEventArgs(nameof(Username));
}
Usage
[GenerateViewModel]
public partial class ViewModel {
[GenerateProperty(OnChangedMethod = nameof(CustomName))] // this point
string username;
void CustomName() { }
}
Original
partial class ViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler? PropertyChanged;
protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
public string? Username {
get => username;
set {
if(EqualityComparer<string?>.Default.Equals(username, value)) return;
username = value;
RaisePropertyChanged(UserNameChangedEventArgs);
CustomName(); // this point
}
}
static PropertyChangedEventArgs UsernameChangedEventArgs = new PropertyChangedEventArgs(nameof(Username));
}
[GenerateViewModel]
partial class ViewModel {
[GenerateCommand (AllowMultipleExecution = true)]
async Task CalculateAsync() {
//...
}
}
Original
partial class ViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler? PropertyChanged;
protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
AsyncCommand? calculateAsyncCommand;
public AsyncCommand CalculateAsyncCommand {
get => calculateAsyncCommand ??= new AsyncCommand(CalculateAsync, null, true, true);
}
}