NotifyOfPropertyChange'在当前上下文中不存在

本文关键字:上下文 不存在 NotifyOfPropertyChange | 更新日期: 2023-09-27 18:06:56

我在下面一段代码上得到一个错误:

public class PathSelectionPageViewModel : ObservableObject, INavigable, INotifyPropertyChanged
{
    private DriveInfo driveSelection;
    public DriveInfo DriveSelection_SelectionChanged
    {
        get
        {
            return driveSelection;
        }
        set
        {
            if (value == driveSelection) return;
            driveSelection = value;
            NotifyOfPropertyChange(() => DriveSelection_SelectionChanged);//must be implemented
        }
    }
}

我得到错误NotifyOfPropertyChange Does not exists in current context。所有的用法都可以;我查了类似的问题,没有找到答案。为什么找不到NotifyOfPropertyChange ?

NotifyOfPropertyChange'在当前上下文中不存在

在Visual Studio的"解决方案资源管理器"中(通常在右侧窗格中),转到使用此方法的项目,并打开"Add Reference"的上下文菜单。

当对话框出现时,导航到您的Caliburn程序集并选择DLL。

然后返回到正在发生编译器错误的项目,并添加适当的'using'语句,以便编译器可以找到它。

定义如下函数:

    private void RaisePropertyChanged(string propName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(propName));
        }
    }

,然后使用

 set
    {
        if (value == driveSelection) return;
        driveSelection = value;
        RaisePropertyChanged("DriveSelection_SelectionChanged");
    }

我写了它,例如在你之前的问题中,它不是现成的:)你必须自己写或使用框架(Caliburn)。Micro(例如),在这里定义它。更简单的变体:

 public class PathSelectionPageViewModel : ObservableObject, INavigable, INotifyPropertyChanged
{
    private DriveInfo driveSelection;
    public DriveInfo DriveSelection_SelectionChanged
    {
        get
        {
            return driveSelection;
        }
        set
        {
            if (value == driveSelection) return;
            driveSelection = value;
            NotifyOfPropertyChange("DriveSelection_SelectionChanged");
        }
    }
     public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
}

我从Intellisense得到了相同的错误消息,解决方案是删除. so文件。在这里找到的:https://stackoverflow.com/a/33477634/2523899