1024programmer Asp.Net CommunityToolkit.Mvvm8.1 viewmodel source generator writing (3)

CommunityToolkit.Mvvm8.1 viewmodel source generator writing (3)

CommunityToolkit.Mvvm8.1 viewmodel source generator writing (3)

Navigation of this series of articles
  1. https://www.cnblogs.com/aierong/p/17300066.html
  2. https://github.com/aierong/WpfDemo (self-Demo address)

I hope the knowledge mentioned can give you some hints, and welcome to communicate and correct.
Author: aierong
Source: https://www.cnblogs.com/aierong

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
 {

 }

ObservableProperty tag attribute

Defining properties is so simple: a [ObservableProperty] tag is done

/*
 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 );
 // }
 //}

NotifyPropertyChangedFor notification dependency property

[NotifyPropertyChangedFor( nameof( Caption ) )] flag: After LastName changes, notify Caption

public string Caption
 {
     get
     {
         return string.Format( "Title:{0}-{1}" , Title , LastName );
     }
 }


 [ObservableProperty]
 [NotifyPropertyChangedFor( nameof( Caption ) )]
 private string lastName = "abc";

NotifyCanExecuteChangedFor notification dependent command

After the attribute IsEnabled changes, notify the command: ButtonClickCommand

/*
         [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();
 //}

Command

The RelayCommand flag defines a command, so simple

/*
 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;
}

}
}

navigation

https://github.com/aierong/WpfDemo/tree/main/WpfDemoNet6 (project address)

https://github.com/aierong/WpfDemo/blob/main/WpfDemoNet6/Demo/DataViewModel2.cs (code address)

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



banner

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!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/communitytoolkit-mvvm8-1-viewmodel-source-generator-writing-3/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索