1024programmer Asp.Net CommunityToolkit.Mvvm8.1 message notification(4)

CommunityToolkit.Mvvm8.1 message notification(4)

CommunityToolkit.Mvvm8.1 message notification (4)

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

In order to separate different modules of the application and reduce references between modules, CommunityToolkit.Mvvm provides a message notification function, which can facilitate data transfer between modules.

Send a message

Method: WeakReferenceMessenger.Default.Send

The official recommendation is to use ValueChangedMessage to encapsulate data transmission

//Send send message
 WeakReferenceMessenger. Default. Send( "qq1" );

 //Special attention: directly passing the value can only be a reference type, and the value type cannot be compiled successfully (for example: the following 2 sentences will not work)
 //WeakReferenceMessenger.Default.Send(11, "token_1");
 //WeakReferenceMessenger.Default.Send(true, "token_1");

 //The above is also possible, but the official recommendation is to use ValueChangedMessage to encapsulate data transfer
 WeakReferenceMessenger.Default.Send<ValueChangedMessage , string>( new ValueChangedMessage( "qq1 from UserControlLeftViewModel") , "token_1" );

It is recommended to bring the token name when sending messages, so that it is convenient for subscription receivers to filter data

WeakReferenceMessenger.Default.Send<ValueChangedMessage , string>( new ValueChangedMessage( "qq1 from UserControlLeftViewModel") , "token_1" );  

Send message passing object

 public class MyUserMessage
     {
         public string UserName
         {
             get; set;
         }

      

         public int Age
         {
             get; set;
         }
     }
//Send sends a complex data
 var _data1 = new MyUserMessage() { Age = 18 , UserName = "qq" };
           
 WeakReferenceMessenger.Default.Send<ValueChangedMessage , string>( new ValueChangedMessage( _data1 ), "token_class" );

Send a message and return a response value

 /// 
     /// Must inherit RequestMessage RequestMessage means the type of returned data is string
     /// 
     public class MyMessage : RequestMessage
     {
         public string Datas;

         public int Ids;
     }
 //result receives the returned value
             //MyMessage must inherit from RequestMessage
             var _data2 = new MyMessage() { Datas = "qqq" , Ids = 100 };
             var result1 = WeakReferenceMessenger.Default.Send( _data2, "token_Response");
             if ( result1 != null )
             {
                 //Get the returned value
                 var val = result1. Response;

                 Name = val;

             }

Receive subscription news

Two ways to receive:

Method 1. Inherit ObservableRecipient

Method 2. Implement interface IRecipient

Method 1 is more flexible than Method 2, and Method 1 is recommended

Receiver remember to set IsActive=true to receive the message

We receive message data in OnActivated of vm

 [ObservableProperty]
         private string name = "hello";

         public UserControlTopViewModel ()
         {
             //Note that you have to write in this way before you can answer
             IsActive = true;
         }
 protected override void OnActivated ()
         {
             //RegisterThe first type is generally its own type, and the second is the type of received data
             //The first parameter of the Register method is generally this, and the second parameter is a method that can get the received value
             Messenger.Register( this , ( r , message ) =>
             {
                 Name = Name + "Received msg:" + message;
             } );

             //RegisterThe first type is generally its own type, the second is the type of received data, and the third is the type of token data
             //The first parameter of the Register method is generally this, the second parameter is token, and the third parameter is a method that can get the received value
             //Messenger.Register( this , "token_1" , ( r , message ) =>
             //{

             // Name = Name + "Received msg:" + message;
             //} );
             //ValueChangedMessage
             Messenger.Register<UserControlTopViewModel , ValueChangedMessage , string>( this , "token_1" , ( r , message ) =>
             {

                 Name = Name + "Received msg:" + message.Value;
             } );



             //Messenger.Register( this , "token_class" , ( r , user ) =>
             //{
             // Name = Name + "Received msg:" + user.UserName + user.Age;
             //} );
             Messenger.Register<UserControlTopViewModel , ValueChangedMessage , string>( this , "token_class" , ( r , user ) =>
             {
                 Name = Name + "Received msg:" + user.Value.UserName + user.Value.Age;
             } );


             Messenger.Register( this , "token_Response" , ( r , message ) =>
             {
                 Name = Name + "Received msg:" + message.Datas;

                 //Reply is a reply, so you can return a value
                 message.Reply( "UserControlTopViewModel gives you the return value" );

             } );
         }

Self Demo address:

https://github.com/aierong/WpfDemo/tree/main/WpfDemoNet6/MessengerDemo

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-message-notification4/

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