简体   繁体   English

RelayCommand不刷新执行/无法执行更改

[英]RelayCommand is not refreshing execute/canexecute changes

I'm newbee in mvvm (and mvvlight of course). 我是mvvm中的newbee(当然是mvvlight)。 I have 3 modelviews (a MainWindow which have a container, and another 2 modelviews (Login and Menu)). 我有3个modelviews(一个具有容器的MainWindow和另一个2个modelviews(登录和菜单))。 In the LoginModelView, when the user login is successfully, this call the MenuViewModel (With Messenger.Default) changing the page in the MainWindow container. 在LoginModelView中,当用户成功登录时,这将调用MenuViewModel(带有Messenger.Default)来更改MainWindow容器中的页面。 All is alright until that, then i call a Message.Default.Send sending a object from LoginModelView to MenuModelView which is correctly listened, catching the object associed and executing the method associated (ConfiguraMenu) wich define a RelayCommand (checked line by line and the method is executed without any exception) but the problem is this RelayCommand is not working until i back to the LoginViewModel and i login again. 在此之前一切都很好,然后我调用Message.Default.Send将一个对象从LoginModelView发送到MenuModelView,该对象被正确侦听,捕获关联的对象并执行关联的方法(ConfiguraMenu),其中定义了RelayCommand(逐行检查方法被执行,没有任何异常),但问题是直到我回到LoginViewModel并再次登录后,此RelayCommand才起作用。 I try CommandManager.InvalidateRequerySuggested() and is not working either. 我尝试使用CommandManager.InvalidateRequerySuggested(),但也不起作用。

This is the code for the LoginViewModel: 这是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);
            }
        }

This code is for the MenuViewModel: 此代码用于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();
        }

I tried to hardcode the CanExecute with true but the Execute is still without work until back and login again. 我尝试使用true对CanExecute进行硬编码,但是Execute仍然无法工作,直到返回并再次登录。

I hope you can help me (i'm scratching my head for various days with none result). 希望您能为我提供帮助(我几天来都挠头,没有结果)。

MvvmLight provides two different RelayCommand classes in two different namespaces: MvvmLight在两个不同的名称空间中提供了两个不同的RelayCommand类:

  • Galasoft.MvvmLight.Command
  • Galasoft.MvvmLight.CommandWpf

Make sure, that you are using the correct namespace Galasoft.MvvmLight.CommandWpf in your WPF application. 确保在WPF应用程序中使用了正确的命名空间Galasoft.MvvmLight.CommandWpf

There was a bug in MVVMLight, which resulted in not working CanExecute() behavior. MVVMLight中存在一个错误,导致无法正常运行CanExecute()行为。 They fixed it with the new .CommandWpf namespace in MVVMLight Version V5.0.2. 他们使用MVVMLight版本V5.0.2中的新.CommandWpf命名空间修复了该问题。

You can also check out this GalaSoft blog post and the change log for further information. 您也可以查看此GalaSoft博客文章更改日志以获取更多信息。

You try to bind the CanExecute to a propertie. 您尝试将CanExecute绑定到属性。

So my guess is you didn't use RaisePropertie Changed in this propertie. 所以我的猜测是您没有在此属性中使用RaisePropertie Changed。 You must have something like: 您必须具有以下内容:

        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); }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM