Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Why won't my PropertyChange not be null for an iNotifyPropertyChanged?

$
0
0

I am trying to bind a label's text to a ViewModel property, but for some reason the PropertyChange is always null when RaisePropertyChanged is fired. The viewmodel's property value does change, and the binding context is set and shows the new appropriate value,but it does not update during running, as the PropertyChanged is never not null.

The pertinent code for my view and label (resultLabel) is:

On init:

public InitStatusViewModel viewModel = new InitStatusViewModel{StatusString="Pending..."};
    resultLabel.BindingContext = viewModel;
        resultLabel.SetBinding (Label.TextProperty, "StatusString");

Button click:

returnButton.Clicked += async (object sender, EventArgs e) => {
                    await DoIt();
                };

Task:

private async Task DoIt()
        {
            viewModel.StatusString = "Starting...";
            await Task.Run(() =>
                {
                    for (int i = 0; i <= 100000000; ++i)
                    {
                    }
                });
            viewModel.StatusString = "Done!";
        }

Base View Model:
public class ViewModel : INotifyPropertyChanged
{
public ViewModel ()
{
}

        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property. 
        // The CallerMemberName attribute that is applied to the optional propertyName 
        // parameter causes the property name of the caller to be substituted as an argument. 
        protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

InitStatusViewModel:

public class InitStatusViewModel : ViewModel
    {
        private string _statusString;
        public string StatusString
        {
            get{
                return _statusString;
            }

            set{
                _statusString = value;
                RaisePropertyChanged ();
            }
        }
    }

Any help is much appreciated.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>