WPF IOException Cannot locate resource

Viewed 67407

I have a WPF application.

The page that opens when the app runs in MainWindow.xaml, as set in the StartupUri attribute of the App.xaml file. This page opens fine.

However, if I try to open any other windows using the Show or ShowDialog method I get an IOException in the InitializeComponent method saying "Cannot locate resource 'Window1.xaml'" (or whatever the file is called). This happens with every single window I create. I've searched online but all the solutions seem to say "make sure the StartupUri attribute of the App.xaml is correct" and mine is, hence MainWindow opening.

Any idea what's going on?

18 Answers

I had this problem when the "AssemblyName" and the "Default Namespace" on the project settings had the same value. Changing the AssemblyName to something else solved the problem.

My issue was quite trivial: The Build Action of my file was set to "None".

If you don't set it to "Resource" this will be the exception.

To resolve this issue please go to App.Xaml and change the StsrtUpUri which you want to run when the application run. enter image description here

Change the startup Uri enter image description here

And if the Xaml is inside any Folder you can add as follow

StartupUri="View/MyView.xaml"

If this helps anyone, I was facing this problem without any obvious problem in the resource path. One thing was that I was using this in a WPF Control Library which was then referenced by the main application assembly.

I simply changed my simple URLs (i.e. file names) to pack:// URIs as everything started to work correctly. Like this:

Source="pack://application:,,,/MyLib;component/SettingsPage.xaml"

instead of:

Source="SettingsPage.xaml"

You may have renamed your namespace globally (entire project/solution etc.) or locally, but your solution obj\Debug folder some content (xaml classes, ending w/ [original_xaml_file_name].g.i.cs) did not take your changes. Next time make sure select "Entire Solution ( Including External Items )" if this was your case.

Simply go to Build and Then "Rebuild" and "Clean"

Related