我是mvvm的newbee (当然还有mvvlight )。我有3个模型视图(一个MainWindow有一个容器,另外2个模型视图(登录和菜单))。在LoginModelView中,当用户登录成功时,调用MenuViewModel (使用Messenger.Default)更改MainWindow容器中的页面。在此之前一切都很好,然后我调用一个Message.Default.Send,将一个对象从LoginModelView发送到MenuModelView,该对象被正确侦听,捕获关联的对象并执行关联的方法(ConfiguraMenu),定义一个RelayCommand (逐行检查,该方法执行时没有任何异常),但问题是这个RelayCommand直到我返回到LoginViewModel并再次登录时才能工作。我尝试了CommandManager.InvalidateRequerySuggested(),但也不起作用。
这是LoginViewModel的代码:
//This method is called when the user press the login button. No problem with this
public void ActionVerificaUsuario()
{
Miusuario = db.getUsuario(Txtusuario, Txtpassword);
if (Miusuario.esUsuario())
{
Messenger.Default.Send(new MoveToViewMessage(Page.MenuView));
Messenger.Default.Send((UsuarioModel)Miusuario);
}
}此代码用于MenuViewModel:
public RelayCommand AbreExeClaseCommand { get; private set; }
public MenuViewModel()
{
Messenger.Default.Register<UsuarioModel>(this, usuario_recibido => {Miusuario = usuario_recibido;ConfiguraMenu(); });
}
private void ConfiguraMenu() {
Mimenu = new MenuModel(Miusuario);
AbreExeClaseCommand = new RelayCommand(() => { Messenger.Default.Send(new MoveToViewMessage(Page.NeverReachedView)); }, () => Mimenu.Sw_reportes);
CommandManager.InvalidateRequerySuggested();
AbreExeClaseCommand.RaiseCanExecuteChanged();
}我尝试将CanExecute硬编码为true,但在返回并再次登录之前,执行仍然没有任何工作。
我希望你能帮助我(我已经挠头好几天了,但都没有结果)。
发布于 2017-01-31 03:40:40
MvvmLight在两个不同的命名空间中提供了两个不同的RelayCommand类:
Galasoft.MvvmLight.CommandGalasoft.MvvmLight.CommandWpf 确保您在WPF应用程序中使用了正确的命名空间Galasoft.MvvmLight.CommandWpf。
MVVMLight中有一个错误,导致CanExecute()行为不能正常工作。他们在MVVMLight版本V5.0.2中用新的.CommandWpf命名空间修复了这个问题。
您还可以查看this GalaSoft blog post和the change log以获取更多信息。
发布于 2017-01-31 18:01:49
您尝试将CanExecute绑定到属性。
所以我猜你没有在这个属性中使用RaisePropertie Changed。您必须具备以下内容:
public class MenuModel : ViewModelBase
{
// Other pieces of code....
private bool _sw_reportes;
public bool Sw_reportes
{
get { return _sw_reportes; }
set { _sw_reportes = value;
RaisePropertyChanged(() => Sw_reportes); }
}
}https://stackoverflow.com/questions/41939411
复制相似问题