1

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 ?

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
Rob.Ak
  • 237
  • 4
  • 10
  • Thanks for your comments, but the event probably is fireing, but not the event handler binded in XAML. I'd like to debug what is stopping event handler to run, or maybe it is changed or cleared by something I don't know. – Rob.Ak May 09 '18 at 14:17
  • Are You creating a new window and closing the previous one? Usually `Windows process` (app running) is tightend with the original `WPF's Window` created, but if You somehow passing the process to other WPF's Windows/Views, You might not get re-added the event handler to it. Otherwise I see no other reason why it would get dumped (not called). – Tatranskymedved May 09 '18 at 14:20
  • 1
    About debugging WPF binding: https://stackoverflow.com/questions/9609685/debugging-wpf-events-bindings – Tatranskymedved May 09 '18 at 14:23
  • @Tatranskymedved, no it's a one window app. No other windows are opened. I thought that the DataContext could be cleared, but handler fired from code behind uses the same DataContext taken from window, so it's not being cleared. – Rob.Ak May 09 '18 at 14:23
  • Your issue is not reproducible: https://stackoverflow.com/help/mcve – mm8 May 09 '18 at 14:44
  • @mm8, I know that is not reproducible. That's why I ask for an advice on debugging it. I've just tried "DataBindingErrorLogger" from link pointed in Tatranskymedved's comment, but that didn't show any error either, except standard like "Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')" – Rob.Ak May 09 '18 at 14:55
  • 1
    @Rob.Ak It would be worth trying to copy-paste code to to other solution & try, which part is it making. It doesn't seems to be as simple-looking issue. Most likely You are rewriting some part that is causing it, but it's hard to find. Converter for the debugging seems not really helpful, if the handler is not called. You should look for setter of the command (for any time it is changed) and maybe get some handler for changing the event (hooking new handler, removing the handler). – Tatranskymedved May 09 '18 at 22:11

0 Answers0