命名空间中不存在 Viewmodel

Viewmodel doesn't exist in namespace

如果这是一个愚蠢的问题或者甚至不属于我要问的问题,我很抱歉,但我是 WPF 的新手,我似乎无法掌握它。现在我正在做类似于 https://www.c-sharpcorner.com/article/use-inotifypropertychanged-interface-in-wpf-mvvm/ 的事情,但我 运行 遇到了问题。当我尝试执行我的代码时:

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";
            }
        }
    }
}

我得到的错误是 "The type or namespace name 'ViewModel' does not exist in the namespace 'DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged' (are you missing an assembly reference?)" 和 "The name "MainWindowViewModel“在命名空间 'clr-namespace:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel' 中不存在。” 和 "The type 'VM:MainWindowViewModel' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built." 我遇到第一个错误两次,一次在 MainWindow.g.cs 中,另一次在 MainWindow.xaml 中。其他两个都在 MainWindow.XAML 如果有人能告诉我我做错了什么,那就太好了。这是 XAML 文件:

<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>

我只是尝试使用不同的名称空间并使它们更简单。并且有效。

DPSCalculatorPrototype.ViewModel

namespace DPSCalculatorPrototype.ViewModel
{
    public class MainWindowViewModel : Calculator
    {
        public MainWindowViewModel()
        {
            Damage = "7";
        }
    }
}

DPS计算器原型

namespace DPSCalculatorPrototype
{
    public class Calculator : NotifyPropertyChanged
    {
        private string _damage;

        public string Damage
        {
            get { return _damage; }

            set
            {
                _damage = value;
                RaisePropertyChange("Damage");
            }
        }
    }

    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChange(string propertyName)
        {
            PropertyChanged?.Invoke(this, new
            PropertyChangedEventArgs(propertyName));
        }
    }
}

MainWindow.xaml

<Window x:Class="DPSCalculatorPrototype.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:DPSCalculatorPrototype.ViewModel"
            xmlns:local="clr-namespace:DPSCalculatorPrototype"
            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>

MainWindow.xaml.cs

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

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
        }
    }
}

您看到这些错误的原因是 WPF 正在查找提到的命名空间,但似乎无法找到您要查找的内容。如果您查看 XAML 代码,您会看到以下行:

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

这是声明使用命名空间的内容,因此我们需要将其指向正确的区域。将您的 XAML 更改为如下所示:

<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"
        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>

您收到这些错误是因为您在命名空间声明中有 "ViewModel",而此命名空间不存在,因此其中不存在任何内容。

这是我见过的最糟糕的 "copy and paste" 工作之一... 我不知道从哪里开始。

只是 运行 您必须的应用程序:

  1. 如下更改命名空间 VM;

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

DPSCalculatorPrototype.ViewModel 不存在。

  1. TextBox_TextChanged 不存在于 window 的代码隐藏中。您必须添加方法

    private void TextBox_TextChanged(对象发送者,TextChangedEventArgs e) { //做你的东西 }

在主窗口中 class。

为了避免让您或正在阅读您的代码的人头疼,您 应该

  1. 每个 class.
  2. 使用一个 .cs 文件
  3. 避免在同一个 .cs 文件中嵌套命名空间,并创建复制命名空间结构的文件夹树。在您的代码片段中,只需在根目录中创建一个 UseOf_INotifyPropertyChanged 文件夹并在其中创建 MainWindowViewModel class。
  4. 名称空间的用途必须通过阅读代码才能清楚。创建一个 DPS_Calculator_Prototype.ViewModels 并将所有应用程序视图模型放入其中。