CommunityToolkit.Mvvm8.1 viewmodel usage – old style (2)
0.Description
CommunityToolkit.Mvvm8.1 has a major update function: the source generator function, which greatly simplifies our mvvm code
But this article summarizes the original writing method first, and then summarizes the source generator function in the next article
1. Model definition
Must inherit: ObservableObject
2. viewmodel code implementation
A few key points:
SetProperty is to assign values to properties and notify change notifications
ButtonClickCommand.NotifyCanExecuteChanged(); //Notification command has changed
RelayCommand ButtonClickCommand //Definition command
namespace WpfDemoNet6.Demo
{
public class DataViewModel1 : ObservableObject
{
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 );
}
}
private bool isEnabled = false;
///
/// Is it possible to use
///
public bool IsEnabled
{
get => isEnabled;
set
{
SetProperty( ref isEnabled , value );
// Notify that the command has changed
ButtonClickCommand. NotifyCanExecuteChanged();
}
}
///
/// Order
///
public RelayCommand ButtonClickCommand
{
get;
}
public DataViewModel1 ()
{
//The first parameter of RelayCommand is the command call statement
// Whether the second parameter (optional) is allowed to be used
ButtonClickCommand = new RelayCommand( () =>
{
//Click the button to modify the title
Title = "hello(change)";
} , () =>
{
return IsEnabled;
} );
ButtonClickCommandPar = new RelayCommand( ( double val ) =>
{
Title = $"hello(change):{val}";
} );
}
public RelayCommand ButtonClickCommandPar
{
get;
}
}
}
3. Asynchronous command
The asynchronous command will automatically control the visibility of the control, and provide an IsRunning property to determine whether the asynchronous is complete
public DataViewModel1 ()
{
AsyncButtonClickCommand = new AsyncRelayCommand( RunTxtAsync );
AsyncButtonParClickCommand = new AsyncRelayCommand( RunTxtParAsync );
}
/*
Special Note: The asynchronous command will automatically control the visibility of the control, and provide an IsRunning property to determine whether the asynchronous command is complete
*/
///
/// Order
///
public IAsyncRelayCommand AsyncButtonClickCommand
{
get;
}
private async Task RunTxtAsync()
{
await Task. Delay( 4800 );
Title = "hello(Task changed)";
}
///
/// command (with parameters)
///
public IAsyncRelayCommand AsyncButtonParClickCommand
{
get;
}
private async Task RunTxtParAsync ( double val )
{
await Task. Delay( 4800 );
Title = $"hello(Task change):{val}";
}
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 STRONG>
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!