Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving MainWindow.xaml

Tags:

c#

wpf

xaml

I've built a WPF app, which completely works now. However, to clean it up a bit I wish to move my MainWindow.xaml to the view folder I've created. After I've done this the application won't run and it gives me an "Unknown build error" which doesn't give any info on how to fix it...
What should I change in my MainWindow.xaml to let the app work properly again?

I've already changed

<Window x:Class="projectname.MainWindow">

to

<Window x:Class="projectname.view.MainWindow">

Should I change other stuff as well?

like image 800
user3208179 Avatar asked Aug 24 '14 14:08

user3208179


3 Answers

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"
like image 53
Rohit Vats Avatar answered Nov 02 '22 13:11

Rohit Vats


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.

like image 20
Zulukas Avatar answered Nov 02 '22 13:11

Zulukas


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.

like image 40
AlexMelw Avatar answered Nov 02 '22 14:11

AlexMelw