bas
bas

Reputation: 14972

Unable to load app.xaml using Application.LoadComponent

I reproduced a problem I have in a project where I need to load an App.xaml using Application.LoadComponent in a small solution with just 1 WPF project.

I have an application entry point in a separate class, which creates the application as follows:

[STAThread]
static void Main()
{
    var app = new App();
    var resource = new Uri("/WpfApplication1;component/app.xaml", UriKind.Relative);
    Application.LoadComponent(app, resource);
    app.Run(new MainWindow());
}

An exception is thrown on the method call LoadComponent:

An unhandled exception of type 'System.IO.IOException' occurred in PresentationFramework.dll

Additional information: Cannot locate resource 'app.xaml'.

I can't see what I am doing wrong, also this answer seems to point to the same solution. And this one too.

The assembly name is correct, double checked it before asking this question. I tried the following resource locations, they all result in the same exception:

var resource = new Uri("/WpfApplication1;component/app.xaml", UriKind.Relative);

var resource = new Uri("/WpfApplication1;component/WpfApplication1/app.xaml", UriKind.Relative);

var resource = new Uri("/WpfApplication1;component/WpfApplication1.app.xaml", UriKind.Relative);

Upvotes: 0

Views: 1838

Answers (1)

AnjumSKhan
AnjumSKhan

Reputation: 9827

Change Build Action of your App.xaml file to Page and run. This will also work.

var app = new App();

var resource = new Uri("/WpfApplication1;component/app.xaml", UriKind.Relative); 

Application.LoadComponent(app, resource);            
app.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);

app.Run();

Upvotes: 3

Related Questions