I'm playing around with WPF for the first time. I've created a sample project in VS2013 with the default "App" and "MainWindow" classes. When I run it, it loads up the MainWindow XAML just fine.
As I start to modify the original code, I want to have the Application object subscribe to certain events for the MainWindow but you can't do that until after the window is created. I figured there'd be an event in the Application class that would fire when it was done processing the XAML and creating the window, but so far I've not found it.
If I add handlers for the Application.Startup or Application.LoadCompleted events, they're called before MainWindow is created.
I'm gonna spend this evening reading up on WPF but thought I'd throw the question out here and see if anybody had suggestions.
There are a couple of ways to achieve what you want to do:
Activated
event on Application
then check Application.MainWindow
When you get the first "Activated
" event, you can then check/use the Application.MainWindow
property.....if Activated
occurs again, then it's because your window was deactivated, then reactivated....so depending on what you are doing decide to do your code only on the first activation or on every one.
<Application x:Class="WpfApplication7.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
//--------------------------------------------------------
public partial class App : Application
{
protected override void OnActivated(EventArgs e)
{
WireUp(MainWindow as MainWindow);
}
public void WireUp(MainWindow mainwindow)
{
mainwindow.GotFocus += new RoutedEventHandler(mainwindow_GotFocus);
mainwindow.LostFocus += new RoutedEventHandler(mainwindow_LostFocus);
// ...etc...
}
void mainwindow_GotFocus(object sender, RoutedEventArgs e)
{
// got focus
}
void mainwindow_LostFocus(object sender, RoutedEventArgs e)
{
// lost focus
}
}
StartupUri
in your App.xaml, and create the MainWindow
yourself<Application x:Class="WpfApplication7.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
//--------------------------------------------------------
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
MainWindow mainwindow = new MainWindow();
mainwindow.Show();
WireUp(mainwindow);
}
public void WireUp(MainWindow mainwindow)
{
mainwindow.GotFocus += new RoutedEventHandler(mainwindow_GotFocus);
mainwindow.LostFocus += new RoutedEventHandler(mainwindow_LostFocus);
// ...etc...
}
void mainwindow_GotFocus(object sender, RoutedEventArgs e)
{
// got focus
}
void mainwindow_LostFocus(object sender, RoutedEventArgs e)
{
// lost focus
}
}
Loaded
event of your MainWindow
find your App
via the Application.Current
singletonIn the handling of the "Loaded" event on your MainWindow
, access "something" you provide in your App
class to wire up the events you are interested in. Downside is it makes your window have a tight coupling to your App class.
public partial class App : Application
{
public void WireUp(MainWindow mainwindow)
{
mainwindow.GotFocus += new RoutedEventHandler(mainwindow_GotFocus);
mainwindow.LostFocus += new RoutedEventHandler(mainwindow_LostFocus);
// ...etc...
}
void mainwindow_GotFocus(object sender, RoutedEventArgs e)
{
// got focus
}
void mainwindow_LostFocus(object sender, RoutedEventArgs e)
{
// lost focus
}
}
//--------------------------------------------------------
public MainWindow()
{
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
App app = Application.Current as App;
app.WireUp(this);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With