我有一个奇怪的问题。
我正在使用 GalaSoft 的 MVVM Light 框架,到目前为止一切正常。 我正在使用 Messenger 系统在 ViewModel 之间正常发送消息,直到我尝试执行以下操作:
我有一个发送消息的单例类 GateKeeper
。
此类不是 ViewModel,因此不继承自 ViewModelBase
。
如果它发送一条消息,它不会在任何地方收到。
我尝试了以下方法:
- 让
GateKeeper
继承自ViewModeBase
-> 没有成功。 - 注册
GateKeeper
以接收消息,从而查看它是否会捕获/接收实际从其自身发送的消息 -> 不成功。 - 将
GateKeeper
从 Singleton 更改为正常实例化 -> 不成功 - 创建一个未连接到 View 的 MVVM ViewModel,并让它发送消息,就像
GateKeeper
-> no Success
我所有连接到 View 的 viewModel 都可以发送消息,并且它们将被接收。
在 Messenger 工作之前,似乎必须将 View 模型“链接”到 View ,但 imo。这是一个主要的限制。
以下是当前非常简化的设置。
在 GateKeeper 上调用 ApplicationInitialize 不会触发在 mainviewmodel 或 GateKeeper 类本身上接收到的消息。
我希望有人对这个问题有建议。
谢谢..
示例设置: MainViewModel 构造函数:
public MainViewModel()
{
Messenger.Default.Register<LoadViewMessage>(this, (message) =>
{
if (message.Sender is GateKeeper) CurrentView = message.View;
else if (message.Sender is LoginViewModel) CurrentView = message.View;
else if (message.Sender is MenuItemBarViewModel) CurrentView = message.View;
});
网守:
public class GateKeeper : IGateKeeper
{
private readonly IEmployeeService _employeeService;
#region Implementation of IGateKeeper
public void ApplicationInitialize()
{
Messenger.Default.Send<LoadViewMessage>(new LoadViewMessage(ObjectLocator.MainMapView), this);
}
public void LoginSucceeded(Employee employee)
{
//This is where we retrieve the available services for the current employee
//TODO: add methods for retrieving service info from backend
//Send a message that should make the mainview load the map into its currentview property
Messenger.Default.Send(new LoadViewMessage(ObjectLocator.MainMapView), this);
}
#endregion
public GateKeeper(IEmployeeService employeeService)
{
_employeeService = employeeService;
//Test.. Is not triggered
//Just used for debugging, thus nothing happens inhere.
Messenger.Default.Register<LoadViewMessage>(this, (message) =>
{
if (message.Sender is GateKeeper) ;
else if (message.Sender is LoginViewModel) ;
else if (message.Sender is MenuItemBarViewModel);
});
}
消息类:LoadViewMessage
public class LoadViewMessage : MessageBase
{
public UserControl View { get; protected set; }
public LoadViewMessage(UserControl view, object sender): base(sender)
{
View = view;
}
public LoadViewMessage(UserControl view):this(view, null){}
}
PS: ObjectLocator 是一个 NinJect 类,它处理对象的所有实例化及其生命周期
@更新 LBugnion(MVVM Light 的创建者)指出问题出在 send 方法中,我实际上在该方法中使用了带有 token 的 Send 重载。
@这不适用于我的情况
Messenger.Default.Send(new LoadViewMessage(ObjectLocator.MainMapView), this);
@这会起作用
Messenger.Default.Send(new LoadViewMessage(ObjectLocator.MainMapView, this));
this 应该作为 token
传递给 loadViewMessage 而不是 Send 方法最佳答案
您的问题出在 Send 方法上。您正在使用将 token 作为第二个参数的方法的重载。您正在将“this”作为 token 传递。这意味着您(可能是错误的)使用消息的发送者作为 token 。
如果您使用 token 发送,您还需要使用相同的 token 注册接收方(在这种情况下,与发送方法中使用的实例完全相同)。由于您没有使用 token 注册,因此 Messenger 永远不会发送消息,这是一种优化机制。
我的猜测是您误解了 Send 方法中 token 的用法。如果您愿意, token 在这里只是作为构建“私有(private)消息传递网络”的一种方式,其中两个对象可以使用相同的 token 来注册/发送并建立私有(private)通信。
在您的情况下,如果您想将 Sender 与消息一起发送,则需要将 Sender 保存在消息本身中,这就是 MessageBase 和派生类所做的。
希望对你有帮助
劳伦特
关于silverlight - 从未收到 Mvvm Light Messenger 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351924/