Quantcast
Channel: MVVM Light Toolkit
Viewing all articles
Browse latest Browse all 1826

Commented Unassigned: Closures not working with RelayCommand [7721]

$
0
0
I'm using `RelayCommand` with a WPF project (.NET 4.6.1), and if the provided action contains local variables (closures), the command fails silently (the action is not executed and no exception is thrown).

This code fails:

```
public ICommand GenerateCommand { get; private set; }

public MainWindowViewModel(FileDialogPresenter fileDialogPresenter, TextFileProcessor textFileProcessor)
{
GenerateCommand = new RelayCommand(() => GenerateJsonFiles(fileDialogPresenter, textFileProcessor));
}

private void GenerateJsonFiles(FileDialogPresenter fileDialogPresenter, TextFileProcessor textFileProcessor)
{
// do work
}
```

This also fails:

```
public ICommand GenerateCommand { get; private set; }

public MainWindowViewModel(FileDialogPresenter fileDialogPresenter, TextFileProcessor textFileProcessor)
{
Action generateAction = () => GenerateJsonFiles(fileDialogPresenter, textFileProcessor);
GenerateCommand = new RelayCommand(generateAction);
}

private void GenerateJsonFiles(FileDialogPresenter fileDialogPresenter, TextFileProcessor textFileProcessor)
{
// do work
}
```

This works:

```
private readonly FileDialogPresenter _fileDialogPresenter;
private readonly TextFileProcessor _textFileProcessor;

public ICommand GenerateCommand { get; private set; }

public MainWindowViewModel(FileDialogPresenter fileDialogPresenter, TextFileProcessor textFileProcessor)
{
_fileDialogPresenter = fileDialogPresenter;
_textFileProcessor = textFileProcessor;
GenerateCommand = new RelayCommand(GenerateJsonFiles); // no closures
}

private void GenerateJsonFiles()
{
// do work (uses fields instead of arguments)
}
```
Comments: Hi, While I understand your frustration, I must underline that almost no one complained about the lack of support for closure in the Messenger, which has been published about 9 years ago. So I would say that most of the usage of lambdas is probably not what you think it is. It seems that more people are using closures with relaycommands, which sounds plausible to me. That said. I understand the issue and I will think of a way to circumvent it. The biggest issue is that closures are not captured by the WeakAction and cannot be dehydrated / rehydrated. So in essence, the compromise we could have is that, if you want to use closure, you would lose the WeakAction altogether (which would be an opt-in scenario). Agreed on documenting it. I will add a note in the Messenger's Register method's XML comments now, and in the RelayCommand. This is a temporary measure, and hopefully I can find a better way to handle this. I remember the days when people were super afraid to use closure because no one really understood how they worked. Seems we came a long way and no with lambdas closures are more popular than ever before (which is actually a good thing, apart from the headache they are causing me now). Take care Laurent

Viewing all articles
Browse latest Browse all 1826

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>