Search code examples
c#wpfdatacontextinotifypropertychangedinheriting-constructors

Viewmodel doesn't exist in namespace


I'm sorry if this is a stupid question or doesn't even fall under what I'm asking, but I'm new to WPF and I can't seem to get the hang of it. Right now I'm doing something akin to https://www.c-sharpcorner.com/article/use-inotifypropertychanged-interface-in-wpf-mvvm/ and I've run into a problem. When I try to execute my code:

namespace DPS_Calculator_Prototype
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow() {
            InitializeComponent();
            }
    }
    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChange(string propertyName) {
            PropertyChanged?.Invoke(this, new 
PropertyChangedEventArgs(propertyName));
        }
    }


        public class Calculator: NotifyPropertyChanged
        {
            private string _damage;
            public string Damage {
                get { return _damage; }
                set {
                    _damage = value;
                    RaisePropertyChange("Damage");
                }
            }
        }

    namespace UseOf_INotifyPropertyChanged
    {
        public class MainWindowViewModel : Calculator
        {
            public MainWindowViewModel() {
                Damage = "7";
            }
        }
    }
}

I get the error that "The type or namespace name 'ViewModel' does not exist in the namespace 'DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged' (are you missing an assembly reference?)" and "The name "MainWindowViewModel" does not exist in the namespace 'clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel'." and "The type 'VM:MainWindowViewModel' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built." I get the first error twice, once in MainWindow.g.cs and another in MainWindow.xaml. The other two are all in MainWindow.XAML If anyone can tell me what I'm doing wrong then that would be great. Here's the XAML file:

<Window x:Class="DPS_Calculator_Prototype.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:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel"
    xmlns:local="clr-namespace:DPS_Calculator_Prototype"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <VM:MainWindowViewModel x:Name="VMMainWindow">
        </VM:MainWindowViewModel>
    </Window.DataContext>

    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" 
         Text="{Binding Damage}" VerticalAlignment="Top" Width="120" 
         Margin="78,28,0,0" TextChanged="TextBox_TextChanged"/>
    </Grid>
</Window>

Solution

  • That's one of the worst "copy and paste" jobs I've ever seen guy... I don't know where to start.

    Just to run the application you MUST:

    1. change the namespace VM as follows;

      xmlns:VM="clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged"

    DPSCalculatorPrototype.ViewModel doesn't exist.

    1. The TextBox_TextChanged doesn't exist inside the codebehind of the window. You must add the method

      private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { //Do your stuffs }

    in the MainWindow class.

    In order to avoid headaches to you or who is reading your code, you SHOULD

    1. use one .cs file for each class.
    2. Avoid to nest namespaces inside the same .cs file and create a folder tree that replicates the namespace structure. In your snippet just create a UseOf_INotifyPropertyChanged folder inthe root and create the MainWindowViewModel class inside that.
    3. The purpose of a namespace must be clear reading the code. Create a DPS_Calculator_Prototype.ViewModels and put all application viewmodel inside it.