WPF CommunityToolkit.Mvvm MessengerCommunication

environment

insert image description here

CommunityToolkit.Mvvm Messenger
Cold in October: How to use Messenger in CommunityToolkit.Mvvm to communicate between ViewModels

WeakReferenceMessenger

I will only talk about a simple weak Messenger here. This is packaged by Toolkit for us. It seems that there is garbage collection or something in it. I didn’t study how it works. I will simply explain how to use it here

method introduction

  • WeakReferenceMessenger.Default.Register
    • subscribe news
  • WeakReferenceMessenger.Default.Send
    • receive message

In order to ensure one-to-one, we need to use the record structure when registering and sending == (I interviewed it later, of course, it can be used without, just a single class) ==. Because this is subscribed and sent according to the name of the type.
C# 9.0: Introduction to the Records structure

Send without callback subscription

//声明record
//类型可以自定义,是通过record的类名自动订阅和发送的
public record LogMessage(string msg);

///在viewModel里面订阅
 public partial class ListViewModel 
 {
    
    



     public ListViewModel()
     {
    
    
     	//在构造函数里面订阅。注意订阅一定是你接受的消息类型
     	//第一个是订阅者,默认是本身,就是this
         WeakReferenceMessenger.Default.Register<LogMessage>(this, Recive);
     }
	//recipient 就是把订阅者,也就是ViewModel传给你,一般用不上
	//第二个参数是你的Recive的值
     public void Recive(object recipient, LogMessage msg)
     {
    
    
         Debug.WriteLine("ListViewModel:" + msg.msg);
     }
 }


send

//发送和接受的类型一定是一致的
WeakReferenceMessenger.Default.Send<LogMessage>(new LogMessage("lala"));

Token distinction

WeakReferenceMessenger.Default.Register<TMessage, Ttoken>
can pass in two generic types in it

//订阅
WeakReferenceMessenger.Default.Register<LogMessage,string>(this,"token", Recive);

//发送
//注意,这里的token必须完全一致
WeakReferenceMessenger.Default.Send<LogMessage,string>(new LogMessage("发送信息"),"token");


Here Token recommends using the enum enumeration type, which is convenient for static compilation and error correction

There is a callback subscription sent

If you want to add a callback function, you need ViewModel to inherit the IRecipient<RequestMessage<TMessage>> interface. TMessage is the same as above, and it is a subscription object

    public partial class LogViewModel : ObservableObject,IRecipient<RequestMessage<LogMessage>>
    {
    
    
        [ObservableProperty]
        private string title = "控制台界面";



        public LogViewModel()
        {
    
    
        	//接口必须实现
            WeakReferenceMessenger.Default.Register(this);
        }
		
		//消息返回
        public void Receive(RequestMessage<LogMessage> message)
        {
    
    
            
            Debug.WriteLine("我接受到了消息"+ message.Response.msg);

            message.Reply(new LogMessage("我返回的消息"));
        }

    }

But I feel that it is not very useful, because there will be no Token, and the type of callback is consistent with the type passed in. This is too rigid. You can define an Action in the record to call back. I won’t explain it here.

    public record LogMessage(string msg,Action<string> callback);

Guess you like

Origin blog.csdn.net/qq_44695769/article/details/132649962