1024programmer Asp.Net CommunityToolkit.Mvvm8.1 viewmodel use – old style (2)

CommunityToolkit.Mvvm8.1 viewmodel use – old style (2)

CommunityToolkit.Mvvm8.1 viewmodel usage – old style (2)

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

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



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-use-old-style-2/

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
首页
微信
电话
搜索