As others have pointed out,
Application.xaml
and
Application.xaml.vb
are the application entry point.
By default,
Application.xaml
points to the
MainWindow.xaml
for startup. Here is what it looks like for a new project:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
If you have ever tried to move the
MainWindow.xaml
to its own folder, if you do not modify the
StartupUri
, your application will not start.
If you want to run code before the
MainWindow.xaml
, then you need to modify the
Application.xaml
and remove the
StartupUri
. See below:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<Application.Resources>
</Application.Resources>
</Application>
Now it is your responsibility. We do this in the
Application.xaml.vb
file:
Class Application
Public Sub New()
AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf OnUnhandledException
End Sub
Protected Overrides Sub OnStartup(e As StartupEventArgs)
Dim window = New MainWindow()
window.Show()
End Sub
Protected Overrides Sub OnExit(e As ExitEventArgs)
MyBase.OnExit(e)
End Sub
Private Sub OnUnhandledException(sender As Object, e As UnhandledExceptionEventArgs)
MessageBox.Show(
DirectCast(e.ExceptionObject, Exception).Message,
"Unhandled Error",
MessageBoxButton.OK,
MessageBoxImage.Stop)
End Sub
End Class
Hope this helps!