如果这是一个愚蠢的问题,或者甚至不属于我要问的问题,我很抱歉,但我是 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";
}
}
}
}
我收到错误消息“名称空间‘DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged’中不存在类型或名称空间名称‘ViewModel’(您是否缺少程序集引用?)”和“名称空间‘clr’中不存在名称‘MainWindowViewModel’ -命名空间:DPS_Calculator_Prototype.UseOf_INotifyPropertyChanged.ViewModel'。” 和“找不到类型'VM:MainWindowViewModel'。验证您没有丢失程序集引用并且所有引用的程序集都已构建。” 我两次收到第一个错误,一次在 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>