进一步学习 CommunityToolkit.Mvvm 之 Messenger

一、带token

1. 订阅消息

WeakReferenceMessenger.Default.Register<UserMessage, string>(this, "MyToken", (r, m) =>
{
});

2. 发送消息

WeakReferenceMessenger.Default.Send(new UserMessage(value),"MyToken");

二、某一种数据类型发送接收消息

1. 订阅消息

ViewModel:ObservableRecipient,IRecipient<ValueChangedMessage<bool>>

public void Receive(ValueChangedMessage<bool> message)
{
}

2. 发送消息

WeakReferenceMessenger.Default.Send(new ValueChangedMessage<bool>(value));

参考:

WPF CommunityToolkit.Mvvm Messenger通讯-CSDN博客

Messenger - Community Toolkits for .NET | Microsoft Learn

posted @ 2023-12-18 15:39  无处不在-超超  阅读(673)  评论(0编辑  收藏  举报