Commands stop being executed

梦想与她 提交于 2019-12-05 15:01:52

Is it using RelayCommand from MVVMLight?

If so, you could be hitting a GC issue. RelayCommand internally uses a WeakReference to its callbacks.

If you're passing in an anon function that's not rooted elsewhere, then it could be getting cleaned up when the GC runs.

Most of the time this isn't an issue because the func is a callback to the VM and the VM itself is rooted in the DataContext, the ViewModelLocator or elsewhere. If you're creating Func's that aren't rooted though, it could be an issue.

One way to root those Func's would be to have a List<Func<string>> in your ViewModel, and add them to the list at the same time you create the RelayCommands.

Am I right that you invoke the command from within the command ?

 Command = new RelayCommand(() => { Log.Insert(0, "-------------\n" + CommandName  + "\n"  + (Command.Invoke())); })

Isn't that recursive?

Can you try to remove the invoke from the expression? and why are you invoking it from inside?

Command = new RelayCommand(() => { Log.Insert(0, "-------------\n" + CommandName  + "\n"  + (Command.Invoke())); })

change to:

Command = new RelayCommand(() => { Log.Insert(0, "-------------\n"); })

it can works, but I don't know why can't use the parameter(Command and CommandName) in the Lambda expressions?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!