I am trying to change the MainWindow location in a WPF application from the default startup URI: MainWindow.xaml to Views\MainWindow.xaml. where Views is a folder created inside the project folder.
Uri: this.StartupUri = new System.Uri(@"Views\MainWindow.xaml", System.UriKind.Relative);
I changed the uri and then the application breaks with the following error:
An unhandled exception of type 'System.TypeInitializationException'occurred in PresentationFramework.dll
Additional information: The type initializer for 'System.Windows.Application' threw an exception.
I placed breakpoints and try-catch blocks in the Main method,the InitializeComponent method and the MainWindow constructor to no avail.It crashes and i can't catch the exception.
Main:
public static void Main() {
try
{
wpfTest.App app = new wpfTest.App();
app.InitializeComponent();
app.Run();
}catch(Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
}
}
Does the startupUri have to be changed somewhere else too?It has only one reference :the one in the InitializeComponent method.
To move the MainWindow to a Views folder (namespace), you have to follow this steps
Change the class name in
MainWindow.xamlModify the namespace in
MainWindow.xaml.csModify the
App.xamlMove
MainWindow.xamlto theViewsfolderAnd thats it.
It does not matter which one you do first/last, but you have to do all of them.