问题描述
我正在使用MVVM构建我的第一个应用程序,但我遇到了麻烦.在我尝试建立对象的可观察集合之前,使用Messenger交流其他ViewModel的某些更改是可以的.我有一个包含列表(innerList)的类(myClass). innerLists的元素应该向其父级发送消息(以表明它们已更改或应删除).我正在从innerList对象发送一条消息.此消息已在myClass中注册.
I'm building my first app using MVVM and I stuck. Using the messenger to communicate other ViewModels about some changes was working ok until I tried to build an observablecollection of objects. I have a class (myClass) that contains a list (innerList). Elements of innerLists should send message to it's parent (to communicate that they changed or should be deleted). I'm sending a message from the innerList object. This message is registered in the myClass.
问题是我需要创建myClass对象的ObservableCollection.据我了解发送-接收消息的想法,myClass的每个实例都将接收从innerList元素发送的消息.目标是仅能够在父myClass对象中接收消息,而不能在myClass的每个对象中接收消息.
The problem is that I need to create ObservableCollection of myClass objects. As I understand the idea of sending - receiving messages, every instance of myClass will receive the message sent from the element of innerList. The goal is to be able to receive the message only in the parent myClass object, and not every object of myClass.
我描述的结构要复杂得多,所以这只是情况的草图.问题-是否可以使用Messenger的发送注册"功能仅将消息发送到父对象.也许应该以其他方式解决?
The structure I described is much more complicated so that's just the sketch of the situation. The question - is it possible to send a message only to parent object using Messenger's Send Register functionality. Maybe it should be solved in some other way?
下面是两个类的构建方式.我把它简化为仅此而已.
Below is how the two classes are built. I shortened it to only this what's important here.
public class ObjTypeListVM : ViewModelBase
{
private ObservableCollection<ObjTypeVM> objTypeList = new ObservableCollection<ObjTypeVM>();
public ObjTypeListVM()
{
Messenger.Default.Register<Messages.ObjTypeModifiedMsg>(this, ObjTypeModified);
}
public ObservableCollection<ObjTypeVM> ObjTypeList
{
get { return objTypeList; }
}
public void ObjTypeModified(Messages.ObjTypeModifiedMsg msg)
{
switch (msg.Type)
{...
case "delete":
Delete(msg.ObjType);
break;
default:
break;
}
}
public void Delete(ObjTypeVM objType)
{...
ObjTypeList.Remove(objType);
}
}
public class ObjTypeVM : ViewModelBase
{
private XmlSTSLib.ObjType objType;
int objTypeId;
private RelayCommand deleteItemCmd;
public ObjTypeVM(ObjType _objType, int _objTypeId)
{ ... }
public int ObjTypeId
{
get { return this.objTypeId; }
set { this.objTypeId = value; RaisePropertyChanged("ObjTypeId"); }
}
public RelayCommand DeleteItemCmd
{
get
{
if (deleteItemCmd == null)
{
deleteItemCmd = new RelayCommand(Delete);
}
return deleteItemCmd;
}
}
public void Delete()
{
Messenger.Default.Send<Messages.ObjTypeModifiedMsg>(new Messages.ObjTypeModifiedMsg(this, "delete"));
}
}
推荐答案
假设您使用Galasoft的MVVM-Light(只是一个猜测; P),有几种方法.
Assuming you use MVVM-Light from Galasoft (just a guess ;P), there are several ways.
一个,通过分配两个对象都知道的令牌,并将消息与该令牌一起发送.只有使用该令牌注册的收件人才能收到消息.
One, by assigning a token which both objects know, and sending the message together with that token. Only recipients which registered with that token will receive the message.
第二,指定邮件对象的Target属性,并让收件人检查它.所有注册的收件人都会收到邮件,但是他们可以根据邮件的Target属性选择是否要回应.
Two, specify the Target property of the message object, and have the recipients check it. All registered recipients will receive the message, but they can choose whether they want to react or not based on the Target property of the message.
在使用Messaging系统方面做得很好,这很糟糕;)
Good job on using the Messaging system, it rocks ;)
这篇关于WPF的MVVM-如何只发送消息给父母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!