我是 MVVM Light 的新手,我正在尝试学习和实现 Messenger 类。我已经阅读了许多其他 Messenger 教程和帖子,但仍然无法正常工作。下面是我写的一些代码来帮助我学习。根据我的研究,我认为我已经正确实施了一切,但它不起作用。有谁知道我做错了什么?
public class MainViewModel : ViewModelBase
{
public MainViewModel() { }
private RelayCommand _sendMessage;
public RelayCommand SendMessage
{
get
{
return _sendMessage
?? (_sendMessage = new RelayCommand(
() =>
{
var pInfo = new PersonalInfo { Name = "Some Name", Age = 31 };
Messenger.Default.Send(pInfo);
}));
}
}
}
public class MessageReciever
{
public MessageReciever()
{
Messenger.Default.Register<PersonalInfo>(this,
action =>
{
Debug.WriteLine(action.Age);
Debug.WriteLine(action.Name);
}
);
}
}
public class PersonalInfo
{
public string Name { get; set; }
public int Age { get; set; }
}