If in the XAML file I attach the "Command" button from the following class, clicking the button does not cause DoIt:
class Thing()
{
public Thing(Foo p1)
{
Command = new RelayCommand(() => DoIt(p1));
}
private DoIt(Foo p)
{
p.DoSomething();
}
public ICommand Command { get; private set; }
}
However, it works if I initialize the field from p1 and pass the field as a parameter to a method call inside the lambda:
class Thing()
{
private Foo field;
public Thing(Foo p1)
{
field = p1;
Command = new RelayCommand(() => DoIt(field));
}
private DoIt(Foo p)
{
p.DoSomething();
}
public ICommand Command { get; private set; }
}
Why doesn't the former work, but does the latter work as expected?
Perhaps relevant: How do shutters work behind the scenes? (FROM#)
EDIT. To clarify, the following will also work for me. However, I would still like to know why the second example did what I expected, but the first did not.
class Thing()
{
private Foo field;
public Thing(Foo p1)
{
field = p1;
Command = new RelayCommand(DoIt);
}
private DoIt()
{
field.DoSomething();
}
public ICommand Command { get; private set; }
}
source
share