31

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.

Paul L
  • 2,240
  • 5
  • 36
  • 55
George
  • 411
  • 1
  • 5
  • 6
  • 3
    Did you try `.\ViewModel\MainWindow.xaml`? One question, why is a view located in a VM namespace? Doesn't make sence, since the VMs should not be aware of the views. – DHN Mar 21 '13 at 09:35
  • Yeah but it still does not work. I am not using MVVM The ViewModel Is just a name of a folder like any other, sorry for the confusion there. – George Mar 21 '13 at 12:37
  • Check your namespaces. The path is determined by the project namespace, not by the folder structure. Its possible you have a class in the folder `ViewModel` that has the namespace `MyApplication.ViewModel.ViewModel` – Rachel Mar 21 '13 at 13:10
  • How come when i run the application using Expression Blend it runs just fine? Could anything be wrong with my visual studio? – George Mar 21 '13 at 13:43
  • I just discovered that there could have been some alteration in my xaml code... i don't know what it was but i moved the .xaml windows out of the folder, renamed the folder and put them back in and voila it worked! Thanks for your suggestions though. – George Mar 21 '13 at 14:05
  • I recently ran into an issue where a user had a different interface language set in his windows. After setting his interface language the app worked as excpected. – mbx Oct 09 '14 at 12:30

5 Answers5

60

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

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

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
  • 5
    Thanks! Though it would be extra helpful if you specified exactly where this property is located. For me it was in App.xaml. – birgersp Aug 21 '21 at 10:29
  • Is it me, or when you move something in VB, it automatically gets updated everywhere in the project so you don't run into stupid little things like this that need micromanagement? – J. Scott Elblein Jan 25 '22 at 17:49
20

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!

Alex Krotnyi
  • 2,217
  • 2
  • 12
  • 6
11

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

Libor
  • 3,285
  • 1
  • 33
  • 41
1

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

Steve Obbayi
  • 6,015
  • 5
  • 27
  • 24
jclstefan
  • 77
  • 8
1

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.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Doug J. Huras
  • 637
  • 1
  • 10
  • 19