If I define a variable within a method, then subscribe to an event with a Lambda expression, and use the variable within the body of that expression, is the reference still valid? I don't get a compile time error, but it doesn't seem to be working quite as I'd hoped, either.
See:
void OnAddAssignment()
{
var win = new View.NewTransportationView();
var vm = new NewTransportationViewModel(DateTime.Today);
vm.JobSaved += (s, e) => { win.Close(); };
win.DataContext = new ViewModel.NewTransportationViewModel(DateTime.Today);
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
win.Show();
}
Note: NewTransportationView
inherits from System.Windows.Window
I was hoping when JobSaved
fires, the window would close, and I wouldn't have to save the reference to the NewTransportationView
outside the method.
I realize there are probably dozens of other ways to solve this problem, many or most more elegant and efficient, and while I am certainly open to seeing suggestions, I am more interested in the general behavior and scope rules for this particular application of a Lambda expression.
Bonus question: where do my Window objects live when I call .Show()
on them?