CommunityToolkit.Mvvm8.1 viewmodel source generator writing (3)
Description
The most surprising thing about CommunityToolkit.Mvvm8.1 is the source generator function it provides, which greatly simplifies our mvvm code
We can realize a certain function by marking an attribute, which is very convenient and fast. Recommended
Summary of commonly used tags
1. Inherit ObservableObject and the class tag is partial class
2. Private variable tag property [ObservableProperty]
3. NotifyCanExecuteChangedFor Notify dependency command
NotifyPropertyChangedFor Notify dependency property
br>4.RelayCommand define command
5.OnPropertyChanged manually notify property update
6.ButtonClickCommand.NotifyCanExecuteChanged() manually notify command update
7.OnLastNameChanging OnLastNameChanged a property change
8.OnPropertyChanged all properties change
Define viewmodel
When defining vm, please use partial class and inherit ObservableObject
public partial class DataViewModel2 : ObservableObject
{
}
/*
After the [ObservableProperty] mark, the attribute (named in uppercase) will be automatically generated, for example: the following will automatically generate Title
Note: This private variable name: must start with lowercase, or underscore, or m_
*/
[ObservableProperty]
private string title = "hello";
//public string Title
//{
// get
// {
// return title;
// }
// set
// {
// //title = value;
// //PropertyChanged?.Invoke( this , new PropertyChangedEventArgs( "Name" ) );
// //SetProperty is equivalent to setting the value, and the PropertyChanged notification call
// SetProperty( ref title , value );
// }
//}
public string Caption
{
get
{
return string.Format( "Title:{0}-{1}" , Title , LastName );
}
}
[ObservableProperty]
[NotifyPropertyChangedFor( nameof( Caption ) )]
private string lastName = "abc";
/*
[NotifyCanExecuteChangedFor( nameof( ButtonClickCommand ) )]
NotifyCanExecuteChangedFor is a notification dependent command (trigger command), which is equivalent to ButtonClickCommand.NotifyCanExecuteChanged() in the set;
*/
[ObservableProperty]
[NotifyCanExecuteChangedFor( nameof( ButtonClickCommand ) )]
private bool isEnabled = false;
//public bool IsEnabled
//{
// get => isEnabled;
// set
// {
// SetProperty( ref isEnabled , value );
// // Notify that the command has changed
// ButtonClickCommand. NotifyCanExecuteChanged();
// }
//}
//partial void OnIsEnabledChanged ( bool value )
//{
// //If the above [NotifyCanExecuteChangedFor( nameof( ButtonClickCommand ) )] is not written, you can manually notify the update here
// //ButtonClickCommand. NotifyCanExecuteChanged();
//}
/*
RelayCommand is a definition command, the automatically generated command name is the method name + Command, and initialized
For example: the following will automatically generate ButtonClickCommand
CanExecute is to specify a judgment method to judge whether it is available
*/
[RelayCommand( CanExecute = nameof( CanButton ) )]
void ButtonClick()
{
//Click the button to modify the title
Title = "hello(change)";
}
bool CanButton ()
{
return IsEnabled;
}
//public RelayCommand ButtonClickCommand
//{
// get;
//}
[RelayCommand]
void ButtonClickPar ( double val )
{
Title = $"hello(change):{val}";
}
//public RelayCommand ButtonClickParCommand
//{
// get;
//}
//{
// //Click the button to modify the title
// Title = “hello(change)”;
//} , () =>
//{
// return IsEnabled;
//} );
//ButtonClickParCommand = new RelayCommand( ( double val ) =>
//{
// Title = $”hello(change):{val}”;
//} );
}
[RelayCommand]
void ButtonClickPar ( double val )
{
Title = $”hello(change):{val}”;
}
//public RelayCommand ButtonClickParCommand
//{
// get;
//}
public string Caption
{
get
{
return string.Format( “Title:{0}-{1}” , Title , LastName );
}
}
[ObservableProperty]
[NotifyPropertyChangedFor( nameof( Caption ) )]
private string lastName = “abc”;
/*
Two methods can also be implemented: OnLastNameChanging OnLastNameChanged (Note that only one of the two methods can be implemented, or neither can be implemented (not two at the same time))
*/
//partial void OnLastNameChanging ( string value )
//{
// Debug. WriteLine( value );
//}
partial void OnLastNameChanged ( string value )
{
// You can do some other things, such as: after the property changes, the message notifies XX
Debug. WriteLine( value );
//Note: If the above [NotifyPropertyChangedFor( nameof( Caption ) )] is not written, you can manually notify the property update here
//OnPropertyChanged( nameof( Caption ) );
}
///
/// All attributes changed
///
///
protected override void OnPropertyChanged ( PropertyChangedEventArgs e )
{
base.OnPropertyChanged( e );
// You can get which attribute has changed
var _proname = e.PropertyName;
}
}
}
My Series
A.Sql Server2005 Transact-SQL new weapon learning
B.MCAD learning
C.Code Reading Summary
D.ASP.NET State Management
E.DB (database)
F.WAP
G.WinForm
H.Flex
I hope the knowledge mentioned above can give you some hints, and welcome to exchange and correct.
Author: aierong
Source: http://www.cnblogs.com/aierong
The post is based on “the status quo “Provided without any guarantee, and at the same time, no rights are granted!
The copyright of this article belongs to the author, welcome to reprint!
Original technical articles and experiences, reprint and indicate the source! This is also respect for the original creator!
//{
// Title = $”hello(change):{val}”;
//} );
}
void ButtonClickPar ( double val )
{
Title = $”hello(change):{val}”;
}
//{
// get;
//}
{
get
{
return string.Format( “Title:{0}-{1}” , Title , LastName );
}
}
[NotifyPropertyChangedFor( nameof( Caption ) )]
private string lastName = “abc”;
Two methods can also be implemented: OnLastNameChanging OnLastNameChanged (Note that only one of the two methods can be implemented, or neither can be implemented (not two at the same time))
*/
//{
// Debug. WriteLine( value );
//}
{
// You can do some other things, such as: after the property changes, the message notifies XX
Debug. WriteLine( value );
//OnPropertyChanged( nameof( Caption ) );
}
///
protected override void OnPropertyChanged ( PropertyChangedEventArgs e )
{
var _proname = e.PropertyName;
}
}
A.Sql Server2005 Transact-SQL new weapon learning
B.MCAD learning
C.Code Reading Summary
D.ASP.NET State Management
E.DB (database)
F.WAP
G.WinForm
H.Flex
I hope the knowledge mentioned above can give you some hints, and welcome to exchange and correct.
Author: aierong
Source: http://www.cnblogs.com/aierong
The post is based on “the status quo “Provided without any guarantee, and at the same time, no rights are granted!
The copyright of this article belongs to the author, welcome to reprint!
Original technical articles and experiences, reprint and indicate the source! This is also respect for the original creator!