OnPropertyChanged error, Xamarin app quits

Cdn_EuroCdn_Euro Member ✭✭✭
edited September 2018 in Xamarin.Forms

After adding the OnPropertyChanged for EmailError, the app quits or just nothing happens when I click Register.
If I just set EmailError as:

EmailError {get;set;}

everything works. I am not sure where there error is. I have to properties binded TwoWay with the Register.xaml page. I am thinking OnProperyChanged() gets called at the same time for both properties and then it freezes.

I cannot exactly spot what is wrong. Please let me know if the error is obvious to you.

public class RegisterViewModel: INotifyPropertyChanged
    {
        ApiServices _apiServices = new ApiServices();

        public RegisterViewModel()
        {

            Regions = new List<string>()
            {
                "City1", "City2","City3", "All"
            };
        }

        int regionsSelectedIndex = 0;

        public int RegionsSelectedIndex
        {
            get
            {
                return this.regionsSelectedIndex;
            }
            set
            {
                if (regionsSelectedIndex != value)
                {
                    this.regionsSelectedIndex = value;

                    OnPropertyChanged(nameof(RegionsSelectedIndex));

                    if (regionsSelectedIndex >= 0)
                    {
                        Region = Regions[regionsSelectedIndex];
                    }               
                }
            }
        }
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Region { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }      

        public List<string> Regions { get; set; }

        public string Message { get; set; }

        //public string EmailError { get; set; }


        public string EmailError
        {
            get
            {
                return this.EmailError;
            }
            set
            {
                if (EmailError != value)
                {
                    this.EmailError = value;

                    OnPropertyChanged(nameof(EmailError));

                }
            }
        }



        public Command RegisterUserCommand
        {
            get
            {
                return new Command(async () =>
                {
                    UserDialogs.Instance.ShowLoading("Registering...");

                    var isSuccesful = await ExecuteRegisterAsync();

                    UserDialogs.Instance.HideLoading();

                    UserDialogs.Instance.Alert(Message);

                    if (isSuccesful)
                    {                       
                        await App.Current.MainPage.Navigation.PushAsync(new LoginPage());
                    }
                });

            }
        }

        protected async Task<bool> ExecuteRegisterAsync()
        {
            bool isSuccesful = false;  
            bool areAllEntriesValid = true;

            if ((string.IsNullOrEmpty(Email)))
            {
                areAllEntriesValid = false;
                EmailError = "Email is required.";
            }
            else
            {
                if (!(Regex.IsMatch(Email, AppConstants.EMAIL_REGEX_PATTERN)))
                {
                    areAllEntriesValid = false;
                    EmailError = "Email format is not valid.";
                }
            }


            if (areAllEntriesValid)
            {
                try
                {
                    isSuccesful = await _apiServices.RegisterAsync(Email, Password, ConfirmPassword, FirstName, LastName, Region);

                    if (isSuccesful)
                    {
                        Message = "Registered Successfully.";
                    }
                    else
                    {
                        Message = "There was an error in the registration process.";
                    }

                }
                catch (HttpRequestException ex)
                {
                    isSuccesful = false;
                    Message = ex.Message;
                }
            }
            else
            {
                Message = "One or more of your entries are not valid.";
            }

            return isSuccesful;
        }

        public Command CancelCommand
        {
            get
            {
                return new Command(async() =>
                {
                    await App.Current.MainPage.Navigation.PushAsync(new LoginPage());
                });

            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Best Answer

  • Cdn_EuroCdn_Euro ✭✭✭
    Accepted Answer

    I found my own Error, there its... cant believe I spent an hour when it was so obvious...

    string emailError = string.Empty;
            public string EmailError
            {
                get
                {
                    return emailError;
                }
                set
                {
                    if (emailError != value)
                    {
                        emailError = value;
    
                        OnPropertyChanged(nameof(EmailError));
    
                    }
                }
            }
    

Answers

  • Cdn_EuroCdn_Euro Member ✭✭✭
    Accepted Answer

    I found my own Error, there its... cant believe I spent an hour when it was so obvious...

    string emailError = string.Empty;
            public string EmailError
            {
                get
                {
                    return emailError;
                }
                set
                {
                    if (emailError != value)
                    {
                        emailError = value;
    
                        OnPropertyChanged(nameof(EmailError));
    
                    }
                }
            }
    
Sign In or Register to comment.