WPF IOException Cannot locate resource

我的未来我决定 提交于 2019-11-27 02:29:13

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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"



回答5:


<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




回答6:


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.




回答7:


See my analysis here. There is explained, what is happening and there is workaround too: http://connect.microsoft.com/VisualStudio/feedback/details/759432 ("Analysis of the "Cannot locate resource app.xaml" crash (WPF projects)")




回答8:


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




回答9:


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.




回答10:


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.




回答11:


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

Change the startup Uri

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

StartupUri="View/MyView.xaml"




回答12:


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




回答13:


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​




回答14:


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.




回答15:


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.




回答16:


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"



回答17:


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



来源:https://stackoverflow.com/questions/6518603/wpf-ioexception-cannot-locate-resource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!