0

I'm getting an error on the following piece of code:

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
        }

    }
}

I'm getting the error NotifyOfPropertyChange Does not exists in current context. All usings are ok; I checked similiar questions and found no answer. Any suggestion onwhy it can not find the NotifyOfPropertyChange?

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
szpic
  • 4,346
  • 15
  • 54
  • 85

4 Answers4

1

In the Visual Studio "Solution Explorer" (usually in the right pane), go to the Project where you are using this method and raise the context menu for "Add Reference".

When the dialog appears, navigate to your Caliburn assemblies and select the DLL's.

Then return to your project where the compiler error is happening and add the appropriate 'using' statement so that the compiler can find it.

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
0

Define function like below:

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

and then use

 set
    {

        if (value == driveSelection) return;
        driveSelection = value;
        RaisePropertyChanged("DriveSelection_SelectionChanged");
    }
Nitin
  • 18,344
  • 2
  • 36
  • 53
  • 2
    Why are you telling the OP to reimplement the method with a different name? The method is defined in Caliburn Micro. – Daniel Kelley Jun 06 '14 at 08:39
  • I think the point of using `NotifyOfPropertyChange` is not to have to maintain a string litteral reflecting the property's name – franssu Jun 06 '14 at 08:42
  • @DanielKelley I am sorry but I dont understand this downvote...but how do you know if OP is using Caliburn and not just copy pasted the code of raise notification.. He is imlementing INotifyPropertyChanged on his VM and not using PropertyChangedBase...so it seems to me if he is implementing INotifyPropertyChagned, this function is neccessary – Nitin Jun 06 '14 at 08:44
  • 1
    You google `NotifyOfPropertyChange` and see it exists in the Caliburn library. You then read the comments and see someone also highlights the point the OP is missing a reference. Regardless, this answer is incorrect. – Daniel Kelley Jun 06 '14 at 08:46
  • @DanielKelley The OP said in a comment that he didn't knew that this function was in an external library (Caliburn Micro). So this answer isn't wrong. – Loetn Jun 06 '14 at 08:47
  • 1
    @Loetn Sorry, but your reasoning is off. For a start, they have said that was the issue. Secondly this solution requires the method name to be provided as a string, which is clearly prone to typos and is not type safe. This is an incorrect answer. – Daniel Kelley Jun 06 '14 at 08:48
  • @nit, it's a great answer, but for a different question. There are ZILLIONS of questions on SO where your answer applies, use some of them instead. – Gayot Fow Jun 06 '14 at 08:53
  • 3
    @GayotFow DanielKelley.. ..chill pals.. the OP is implementing INotifyPropertyChanged explicitly... thats the hint to me that he was not using Calburn and copied this code from somewhere...so was just trying to help him... wow – Nitin Jun 06 '14 at 08:58
0

I wrote it for example in your previous question, it is not exists from-the-box :) You must write it yourself or use framework (Caliburn.Micro, for example) where it is defined. More simple variant:

 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));
            }
        }
}
IL_Agent
  • 707
  • 3
  • 16
0

I had the same error message from Intellisense and the solution was to delete the .suo file. Found it here: https://stackoverflow.com/a/33477634/2523899

The incredible Jan
  • 741
  • 10
  • 17