Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF IOException Cannot locate resource

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?

like image 341
lace.john Avatar asked Jun 29 '11 09:06

lace.john


4 Answers

The above did not work for me but what did work was as follows. Open up the App.xaml

<Application x:Class="dotDiff2013.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    </Application.Resources>
</Application>

You then need to change the start-up URI to the fully qualified one. In my case I had moved my MainWindow.xaml to a folder called 'Main', so changing the above URI to

StartupUri="Main/MainWindow.xaml"

Solved my issue.

like image 188
MoonKnight Avatar answered Nov 19 '22 16:11

MoonKnight


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.

like image 14
Dennie Avatar answered Nov 19 '22 17:11

Dennie


If you open up the code-behind for the Window1.xaml file (i.e. Window1.xaml.cs), you can right click on the InitializeComponent method call and select "Goto Definition". There will be code like the following:

/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
    if (_contentLoaded) {
        return;
    }
    _contentLoaded = true;
    System.Uri resourceLocater = new System.Uri("/TestApp;component/mainwindow.xaml", System.UriKind.Relative);

    #line 1 "..\..\..\MainWindow.xaml"
    System.Windows.Application.LoadComponent(this, resourceLocater);

    #line default
    #line hidden
}

If the Uri in the code above is not correct, then you would receive that error you got.

In addition, if the Build Action of the XAML file is not set to "Page", then you would also have that problem. To check this, you can select the file in the Solution Explorer and press F4.

Finally, if something is renaming the XAML file as part of your build process (such as obfuscation), then again you would receive that error.

Other than that, I would try a "Clean Solution" and "Rebuild Solution" to ensure the file with the InitializeComponent definition is rebuilt.

like image 10
CodeNaked Avatar answered Nov 19 '22 17:11

CodeNaked


I had the same issue. The reason for me because I moved the MainWindow.xaml without adjusting the the App.xaml. If you move your MainWindow.xaml for example into a folder called "UI" you have to adjust following line in the App.xaml

         StartupUri="UI/Mainwindow.xaml"
like image 9
gosua Avatar answered Nov 19 '22 17:11

gosua