Jonas Van der Aa
Jonas Van der Aa

Reputation: 1461

Why is Application.OnStartup not being called?

I have a WPF .NET 4 application where I override the OnStartup method in order to process the file passed to my application. However, it seems that this method is not being called when the application runs. I put an exception in there and even a breakpoint and it starts up and completely ignores this.

Am I missing something?

Code for App.xml.cs:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        throw new NotImplementedException();
    }
}

Contents of App.xaml:

<Application x:Class="XGN_Image_Downloader.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>

EDIT: Found it! The x:Class attribute in App.xaml did not match the App.xaml.cs class :) That's what you get for coding while drinking wine. (Thanks to this thread: WPF app startup problems)

Upvotes: 18

Views: 6310

Answers (2)

Chamath Jeevan
Chamath Jeevan

Reputation: 5172

x:Class must be filled with the namespace and exact class name on App.xml.cs

Eg: <Application x:Class="Namespace.ClassName"

Upvotes: 1

Jonas Van der Aa
Jonas Van der Aa

Reputation: 1461

Found it, I had to set the x:Class attribute in App.xaml to the same class as the App.xaml.cs class. This was an error caused by bad refactoring on my side.

Upvotes: 26

Related Questions