George
George

Reputation: 441

My WPF Application cannot locate resource 'Mainwindow.xaml'

I have been working on a WPF application using vb.net and I've recently run into a problem.

When I build my code, the build is successful but when it comes to running the code the System.IO.IOexception throws an exception "Cannot locate resource ViewModel/ViewModel/Mainwindow.xaml" The .Xaml windows are located in a folder called ViewModel hence I don't know where the other ViewModel/ViewModel... path is coming from, isn't it supposed to be viewModel/MainWindow.Xaml? I have tried all possible solutions including cleaning up the Project but it doesn't seem to work.

The only solution that seems to work is if I change the StartupUri from StartupUri = "ViewModel\MainWindow.Xaml" TO StartupUri = "...\MainWindow.xaml" but I doubt if that is the right thing to do.

Upvotes: 34

Views: 41280

Answers (6)

psurikov
psurikov

Reputation: 3458

In addition to what has been answered here, you can use a more convenient URL formats that will prevent these kind of errors when you move App.xaml or MainWindow files to other folders. For example:

StartupUri="/Project;component/MainView/MainView.xaml"

If you specify the full path like above, then it doesn't matter where you move App.xaml, the location of MainView.xaml will remain correct (within a project). And if you move MainView.xaml then it is also clear how to update StartupUri.

Upvotes: 0

Doug J. Huras
Doug J. Huras

Reputation: 647

I renamed the Mainwindow.xaml file to NameList.xaml and had not updated the App.xaml file. Once I did that StartupUri="NameList.xaml", it was fine.

Upvotes: 1

smanookian
smanookian

Reputation: 287

Visual Studio has somehow renamed my MainWindow.xaml to MainWindow(1).xaml, so again I renamed it to MainWindow.xaml

Upvotes: 1

Arijit Mukherjee
Arijit Mukherjee

Reputation: 3875

Change the StartupUri="MainWindow.xaml" to StartupUri="FolderName/MainWindow.xaml"

Solved my problem, when I moved my MainWindow to the View Folder

Upvotes: 63

Alex Krotnyi
Alex Krotnyi

Reputation: 2217

In my case I needed to use pack URI syntax to set SrartUpUri property of my App.xaml file to point to a new location of my MainWindow.xaml, as so:

   <Application x:Class="TrafficLights.Controller.App"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                StartupUri="pack://application:,,,/View/MainWindow.xaml">

More on pack URIs here:

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx

Hope it helps. Good luck!

Upvotes: 20

Libor
Libor

Reputation: 3303

This happened to me few times - always when I moved MainWindow.xaml to different folder and forgot to update StartupUri in App.xaml.

Upvotes: 11

Related Questions