Xaml WPF MVVM NameSpace & New project folder, InitializeComponent (); StartupUri =

I create a folder and organize my files to keep the MVVM pattern clean. Model folder, View folder and ViewModel folder.

It creates namespace problems at many levels.

InitializeComponent () first;

  • does not exist in the current context

Second, StartupUri =

  • Unable to find ressources

I could find answers, but none of them were complete. From the basic situation:

  • I am creating a new WPF C # Project (Lets Name it PROJECT)
  • I am creating 3 new folders.
  • Moving MainView to View folder.

What should be in PROJECT \ app.xaml?

  • x Class = ""
  • StartupUri = ""

What should be in PROJECT \ View \ MainWindow.xaml?

  • x Class = ""

What should be in PROJECT \ View \ MainWindow.xaml \ MainWindow.cs?

  • Namespace

How about PROJECT \ ViewModel \ FooViewModel.cs?

How about PROJECT \ Model \ FooModel.cs?

And why?

Thus, such a question can be completely canceled. Many thanks

+3


source to share


1 answer


App.xaml is the starting point for your application. x: The class must always define the full namespace of the actual class. so in your example for App.xaml it looks like this:

 x:Class="PROJECT.App"
 StartupUri="Viewmodel/MainWindow.xaml"

      

Startup uri defines the relative path to the desired first page. In your case, it would be Viewmodel / MainWindow.xaml.

If you move files from one location to another, you must check the namespace and adjust it accordingly. For your MainWindow it will be like this:



using System.Windows;

namespace PROJECT.Viewmodel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

      

  • XAML
<Window x:Class="PROJECT.Viewmodel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PROJECT"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

      

Be aware to install both the code (.cs) and the xaml file to point to the same namespace. Wish you luck!

+5


source







All Articles