Search code examples
c#wpffunction-exit

WPF application OnExit event


I have a WPF application with a main Window.

In App.xaml.cs, in the OnExit event, I would like to use a method from my MainWindow code behind...

public partial class App
{
    private MainWindow _mainWindow;

    protected override void OnStartup(StartupEventArgs e)
    {
         _mainWindow = new MainWindow();
        _mainWindow.Show();


    }

    protected override void OnExit(ExitEventArgs e)
    {
        _mainWindow.DoSomething();
    }

}

The method :

public void DoSomething()
{
    myController.Function(
       (sender, e) =>
       {

        },

       (sender, e) =>
        {

        }
        );
 }

But I put a breakpoint on the "_mainWindow.DoSomething();" and when I press f11, it doesn't enter into the function and the function does nothing... Am I missing something ?

I'm a beginner, is it possible to do what I need ?

EDIT : post edited


Solution

  • You declared your _mainWindow as the Window class. The Window class does not have a DoSomething function. Change the class of _mainWindow to MainWindow and it should work.

     public partial class App
    {
        private MainWindow _mainWindow;
    
        ...
    }