I'm using MVVM Light in WPF application and trying to bind Window's Closing event
with a command implemented in ViewModel
.
But when I do it in XAML the event handler is called randomly, right after starting the app it is usually firing, but after a 20 or 30 minutes of app running, the app closes immediately without firing "Closing" handler.
<Window
[...]
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
[...]
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<command:EventToCommand Command="{Binding Source={StaticResource Locator},
Path=Main.ExitAppCmd,
Mode=OneWay}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Window>
The command is declared in MainViewModel
as:
public RelayCommand<CancelEventArgs> ExitAppCmd { get; private set; }
and is set to event handler by:
ExitAppCmd = new RelayCommand<CancelEventArgs>((args) => ExitAppHandler(args));
When the app is closed there are no exceptions. I was trying to set breakpoint on the event handler, but in cases when the app closes immediately, the breakpoint is not executed.
When I bind the same handler in code behind, the handler is always fired.
private void Window_Closing(object sender, CancelEventArgs e)
{
var model = DataContext as MainViewModel;
model.ExitAppHandler(e);
}
How can I debug what's wrong with XAML binding ?