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();
}
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.
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...
}
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.
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">
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