1
1
1SLY22020-06-04 16:16:46
C++ / C#
1SLY2, 2020-06-04 16:16:46

Throws Exception when changing PropertyChanged properties?

I'm trying to create a Hamburger Menu without violating the concept of MVVM:
1. I create a variable that will be associated with the SplitView and will change the value of IsPaneOpen

private bool isPaneOpened;
        public bool IsPaneOpened
        {
            get
            {
                return isPaneOpened;
            }
            set
            {
                SetProperty(ref this.isPaneOpened, value);
            }
        }

2. I create a button that will change the value of IsPaneOpened and bind it to a command:
public ICommand PaneOpenedConvert
        {
            get
            {
                return new Command(PaneOpen);
            }
        }
        private void PaneOpen()
        {
            IsPaneOpened = !IsPaneOpened;
        }

After clicking on the button, my PropertyChanged throws an exception:
System.NullReferenceException: "Object reference not set to an instance of an object."

Here is the PropertyChanged implementation:
public event PropertyChangedEventHandler PropertyChanged;
        protected bool SetProperty<T>(ref T Storage, T Value, [CallerMemberName] string Propertname = null)
        {
            if (EqualityComparer<T>.Default.Equals(Storage, Value)) return false;
            Storage = Value;
            PropertyChanged(this, new PropertyChangedEventArgs(Propertname));
            return true;

        }

What is the problem? Help understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2020-06-04
@1SLY2

Add a question mark:

PropertyChanged?(this, new PropertyChangedEventArgs(Propertname));

And then, while there are no subscribers, there will be null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question