Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App OnStartup never gets called

Tags:

wpf

startup

Very simple problem but I'm making no progress so I thought I should ask...

I'm writing a small WPF prototype where I placed the boot up logics where I believe it belongs: In (the overridden) App.OnStartup method.

The problem is the method never gets called and I have no idea why!

I browsed around some and found someone saying the <Application> tag in App.xaml must specify the implementing class (App) in the "x:Class" attribute. I changed it from x:Class="Application" to x:Class="App" but it made no difference.

What am I missing here?

EDIT: Here's the code...

XAML:

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="App"
    ShutdownMode="OnMainWindowClose"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Recources\Brushes\Brushes.xaml"/>
                <ResourceDictionary Source="Recources\Templates\Templates.xaml"/>
                <ResourceDictionary Source="Recources\Styles\GVSStyles.xaml"/>
                <ResourceDictionary Source="Recources\Styles\TimePicker.xaml"/>
                <ResourceDictionary Source="Recources\Icons\GVSIcons.xaml"/>
                <ResourceDictionary Source="Recources\Icons\BottleIcon.xaml"/>
                <ResourceDictionary Source="Recources\Styles\BusyAnimationStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Code behind...

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // lower default framerate from 60 to 20 to save CPU ...
    Timeline.DesiredFrameRateProperty.OverrideMetadata(
        typeof(Timeline),
        new FrameworkPropertyMetadata { DefaultValue = 20 });

    hookUpViews();
    connectToServer();
}
like image 501
Jonas Rembratt Avatar asked Jun 22 '11 15:06

Jonas Rembratt


4 Answers

Edit: Your XAML seems to not be associated with the code behind, the x:Class needs to include the namespace of your App class. e.g. MyWpfApplication.App.


Unless you post some code you just get wild guessing, here's mine: You didn't properly override the method but hide it with a method of the same name and signature.

This is what a working override should look like:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    MessageBox.Show("!");
}

As suggested you can use the Startup event instead, but you don't have to, further the StartupUri will be executed in addition to the code in the override.

like image 108
H.B. Avatar answered Nov 06 '22 07:11

H.B.


As an alternative to @Philippe's answer, you can also wire up in the code-behind:

public App()
{
  this.Startup += new StartupEventHandler(App_Startup);
}

void App_Startup(object sender, StartupEventArgs e)
{
   //do stuff here...
}
like image 26
Brian Driscoll Avatar answered Sep 29 '22 02:09

Brian Driscoll


In my case I renamed the project and the namespace. Then all methods in the code behind didn't fire any more.

The x:Class still showed the old namespace. The App class was just referring another namespace what was out of use, and nothing complained about that.

So, you make short, you have to rename the x:Class, just the way you would do for 'regular' code files.

like image 2
Patrick Hofman Avatar answered Nov 06 '22 06:11

Patrick Hofman


You need the connect the EventHandler :

<Application x:Class="Abc.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
like image 1
Philippe Lavoie Avatar answered Nov 06 '22 07:11

Philippe Lavoie