Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call a function in MainWindow.Xaml.cs from App.Xaml.cs?

This seems doable but for some reason the proper way is not occurring to me. I am new to C# and .NET so I hope this isn't a ridiculous question :)

like image 644
nicky Avatar asked Sep 15 '25 20:09

nicky


1 Answers

Not sure why you would want to do this. It doesn't seem like the best design, but without knowing the details of what you are doing, I can't comment about that. Here's how this could be done:

In App.Xaml.cs:

var main = App.Current.MainWindow as MainWindow; // If not a static method, this.MainWindow would work
main.MyFunction();

Note that you would have to do this AFTER startup. If you want to do it BEFORE startup, you'll need to create the MainWindow object and assign it to this.MainWindow:

var window = new MainWindow();
this.MainWindow = window;

window.Show();

window.MyFunction();
like image 161
Josh G Avatar answered Sep 18 '25 16:09

Josh G