Moving MainWindow.xaml

I just ran into this myself. Here's what I did:

  1. Move your MainWindow.xaml to your new folder. In my case it was /Views.
  2. I like to name all my classes with their namespaces reflecting their folder. So my MainWindow.xaml.cs namespace went from ProjectNamespace to ProjectNamespace.Views
  3. In my MainWindow.xaml, I needed to change x:Class from ProjectName.MainWindow to ProjectName.Views.MainWindow.
  4. In your App.xaml, change the StartupUri to reflect the folder. I went from StartupUri="MainWindow.xaml" to "StartupUri="Views/MainWindow.xaml"`

Doing this allowed me to compile and run my app.


The answer of @Rohit Vats is quite good!

However there's a good point to remember: you have to change all the absolute paths (in XAML) to your resources, prepending them by a / in order to indicate that these paths are relative to the root directory.


Example:

From <Image Source="Assets/Loading.gif">

To <Image Source="/Assets/Loading.gif"> and so on.


You don't need to update class name in xaml file unless you have changed the namespace of your class. Most likely you haven't updated StartupUri for App.xaml. Change it to:

StartupUri="view/MainWindow.xaml"

from

StartupUri="MainWindow.xaml"

Tags:

C#

Wpf

Xaml