WPF IOException Cannot locate resource
Asked Answered
S

18

61

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?

Specialism answered 29/6, 2011 at 9:40 Comment(5)
where you writing the code to open? are providing Window1 in the Startupuri?Lytic
the code to open Window1 was in the click event handler of a button in MainWindow. As I was at the start of the project I decided to delete the project and start again. Copy and pasted the code into the new project and it's working now! the compiler must have got itself confused about something. I say it works, actually the ShowDialog method is now opening the window and immediately closing it, returning false!Specialism
forget my second problem. was me being an idiot! forgot i was setting the visibility property to hidden. everything's working now. don't know what was going on in my original problem, if anyone knows would be interesting to find outSpecialism
pmichaels.net/2015/01/07/…Kursk
In my case, I'd forgotten to write the correct and complete name of the view! Silly me! It has to be like StartupUri="Views/MainWindow.xaml" and I had written StartupUri="Views/MainWindow"Meador
D
80

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.

Devlen answered 18/3, 2013 at 20:9 Comment(3)
I did same and it worked after I restarted the project and rebuilt it.Postmeridian
This solved my issue as well. I had forgotten that I moved my MainWindow.xaml into a Views folder.Biologist
Ha, accidentally typed dots instead of slashes like StartupUri="Main.MainWindow.xaml". This post helped.Helvellyn
R
14

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.

Regorge answered 5/4, 2012 at 8:14 Comment(2)
This helped me as well. Though I swear, I have an exact replica of the project (Beyond Compare app essentially shows only namespace changes, and it works fine).Herman
In fact, if I chose a different name but use it for both "Assembly name" and "Default namespace", it works as well. It seems the problem is in fact the particular Assembly name, while the Default namespace doesn't matter. I can only guess that it might be related to the other project I have in my solution.Herman
C
10

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.

Caputo answered 29/6, 2011 at 11:39 Comment(4)
The URI in the InitializeComponent method was definitely right and I must have cleaned and rebuilt about 100 times (pulling out yet more hair with each attempt). I assume something must have been renaming the XAML during a build. I'm pretty sure visual studio just got confused as creating a new project and copy and pasting the code solved it. Thanks for your helpSpecialism
did you find the answer in the end ?Canula
"Clean Solution" followed by "Rebuild Solution" worked for me. Thanks!Indication
I am attempting to use a WPF form in a Class Library so my code was doing the Uri construction. Copying the code for the Uri allocation from the InitializeComponent to my code solved this problem for me.Sate
S
10

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"
Swelter answered 22/5, 2013 at 16:32 Comment(0)
S
4

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.

Seagoing answered 12/5, 2021 at 6:17 Comment(2)
Welcome to SOF, please comment one line answersKramer
Thanks, in my case was "Embedded Resource", I had to change it to Resource and clean solutionDownright
M
3
<Application x:Class="RuleSetEditor.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="/Forms/RuleEditor.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

here /Forms/ is my folder structure in my project. Make sure this is mentioned in app.xaml

Mithridate answered 22/4, 2013 at 13:53 Comment(1)
My project suddenly stopped loading and through mentioned above exception did rebuild and clean up 100 times didn't worked, checked start-up uri in app.xml and was the same as before, however I have added "/" before my respected folder name where my MainWindow.xml located and it went through with load up.Auberta
N
2

This IOException can be caused by assembly name ambiguity. For example, I named an assembly myproduct.dll, then added a reference to it in the WPF app myproduct.exe. This gave the IOException for mainwindow.xaml.

Navarre answered 4/1, 2012 at 16:15 Comment(1)
Yeah, it was also my case, but I had seen it coming. It's no good to name two different assemblies with the same name if one of them reference the other. For one thing, there's only one pdb file after building a solution, which is bad.Satire
S
2

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"
Selfsupporting answered 10/11, 2019 at 11:54 Comment(0)
J
1

Also if you happen to override OnStartup(StartupEventArgs e) in your app.xaml.cs you must also have to remove the line StartUri="mainwindow.xaml" from app.xaml. Otherwise, you will get "Cannot locate resource 'MainWindow1.xaml'" as well in this case.

Jabez answered 3/5, 2012 at 10:14 Comment(0)
E
1

Check in App.xaml the StartupUri tag (if you moved the MainWindow).

Electromagnetic answered 6/8, 2012 at 14:31 Comment(0)
P
1

Even I had the same problem, first I went on digging up the issue still it was pointing to InitializeComponent(); I finally found out that I updated Resources.resx file contents, but in my application folder I did not updated it. So later copied the new resx file and tried it. Now it works fine.

Just anyone in case come with this issue look at this once.

Precipitin answered 19/6, 2015 at 7:2 Comment(0)
N
1

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"

Nabataean answered 22/9, 2018 at 17:7 Comment(0)
E
0

Find the file app.g.cs and edit it in Notepad, in Visual Studio it will ask you to reload the file, click OK and voila​

Expunge answered 6/5, 2012 at 20:22 Comment(0)
W
0

Make sure you haven't accidentally moved the file MainWindow.xaml Mine had somehow got dragged into Views by mistake - oops

Washstand answered 30/7, 2012 at 3:7 Comment(1)
But even after mooving it it should work rit.if its mooved then where we need to change.Nabataean
D
0

Same issue but yet another solution:

For me my assembly name and default namespace were the same but different from the project name. I actually updated the default namespace and assembly name to match the new project name and it fixed it.

Dawes answered 7/2, 2014 at 21:5 Comment(0)
E
0

I noticed this problem after I localised my application. I ended up with a satellite resource which I did not include in my installer script. So while it worked in Visual Studio, building the installer separately caused this problem.

Including the satellite dll did the trick.

Euratom answered 12/1, 2015 at 2:23 Comment(0)
P
0

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.

Pacemaker answered 4/8, 2020 at 12:24 Comment(0)
M
-2

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

Maxillary answered 23/4, 2019 at 17:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.