Reputation: 1910
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
Upvotes: 0
Views: 8754
Reputation: 116
Class Window does not have the member DoSomething, class MainWindow does (derived from Window).
Either change
private Window _mainWindow;
to
private MainWindow _mainWindow;
or cast your method call like this
((MainWindow)_mainWindow).DoSomething();
Upvotes: 0
Reputation: 7591
your app.cs shoule look like
public partial class App : Application
{
private MainWindow _mainwindow;
public MainWindow mainwindow
{
get { return _mainwindow??(_mainwindow=new MainWindow()); }
set { _mainwindow = value; }
}
protected override void OnStartup(StartupEventArgs e)
{
_mainwindow.Show();
}
protected override void OnExit(ExitEventArgs e)
{
_mainwindow.DoSomething();
}
}
Upvotes: 0
Reputation: 265
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;
...
}
Upvotes: 1