-1

I am trying to start a MVVM pattern and I have this structure:

enter image description here

App.xaml:

<Application x:Class="StereoVisionApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:StereoVisionApp"
             StartupUri="MainView.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:MainViewModel x:Key="StereoVisionApp.MainViewModel"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainViewModel.cs:

namespace StereoVisionApp.ViewModels
{
    class MainViewModel 
    {
        public MainViewModel()
        {

        }
    }
}

I have an error on this line:

<local:MainViewModel x:Key="StereoVisionApp.MainViewModel"/>

Says:

The name "MainViewModel" does not exist in the namespace "clr-namespace:StereoVisionApp". StereoVisionApp C:\Users\Me\source\repos\StereoVisionApp\StereoVisionApp\App.xaml

I have tried restarting million times. any help?

Sandra K
  • 1,209
  • 1
  • 10
  • 21

1 Answers1

2

In Visual Studio, when you create a new folder in the project at creates a new namespace named projectname.foldername by default. (in your case: StereoVisionApp.ViewModels). All the files inside it automatically takes that namespace.

You can either:

  1. Change the namespace in MainViewModel.cs or
  2. Add the new namespace to App.xaml like this:

add this in Application header in App.xaml

xmlns:vm="clr-namespace:StereoVisionApp.ViewModels"

Then use it like this:

<vm:MainViewModel x:Key="StereoVisionApp.MainViewModel"/>

Also note that the x:Key value is a string of your choice (doesn't have to be an exact location, so you can actually write:

<vm:MainViewModel x:Key="mainViewModel"/>

The x:Key value is for future reference only.

Shachar Har-Shuv
  • 666
  • 6
  • 24