我遇到了一个奇怪的问题。
我正在使用GalaSoft的MVVM Light框架,到目前为止一切都运行得很好。 我使用信使系统在ViewModel之间发送消息,一切正常,直到我尝试执行以下操作:
我有一个单例类GateKeeper
,用于发送消息。
这个类不是ViewModel,因此不继承ViewModelBase
。
如果它发送消息,则不会在任何地方接收到。
我已经尝试过以下方法:
- 让
GateKeeper
继承ViewModeBase
-> 没有成功。 - 注册
GateKeeper
以接收消息,从而查看是否会捕获/接收实际从其本身发送的消息 -> 没有成功。 - 将
GateKeeper
从Singleton更改为普通实例化 ->未成功 - 创建一个MVVM ViewModel,未连接到视图,让它像
GateKeeper
一样发送消息 -> 未成功
我所有连接到视图的ViewModel都可以发送消息并接收到它们。
似乎视图模型在信使工作之前必须与视图“链接”,但我认为这是一个主要的限制。
下面是当前非常简化的设置。
调用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;
});
GateKeeper:
门卫:
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);
});
}
Message class: 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应该传递给loadViewMessage而不是作为token传递给Send方法。
GateKeeper
方法了吗?例如,你正在使用非默认构造函数(带参数),它被调用了吗? - jv42