Search code examples
mvvmmauimaui-community-toolkit

Maui Mvvm working with Observable Property Class Model, how to call a class object correctly in a method


Still pretty new to maui and mvvm, I'm trying to figure out of to access elements in a observable property class in a method. For example I have a login class like below:

 public class LoginModel 
    {

       public string Failmessage { get; set; } = string.Empty;


        public string Username { get; set; } = string.Empty;


        public string Password { get; set; } = string.Empty;

        public bool Validusername { get; set; } = true;

        public bool Vaildpassword { get; set; } = true;
  


    }

In my modelview I call the observable property LoginModel loginModel

[ObservableProperty]
        LoginModel LoginModel

        public LoginPageViewModel()
        {
           LoginModel login= new();
        }

I want to check the property value in a method like below but it keeps giving me errors. like Fields with [ObservableProperty] should not be directly referenced, and the generated properties should be used instead.

 if ((string.IsNullOrEmpty(LoginModel.Username)) || (string.IsNullOrWhiteSpace(LoginModel.Username)))
            {
                IsBusy = false;
                LoginModel.Validusername = false;

                return;
            }

Am I doing something wrong here?

I want to be able to access the element of the class in a method.


Solution

  • From your code, I found that you should have used nuget CommunityToolkit.Mvvm.

    And the problem is that you didn't use CommunityToolkit.Mvvm correctly.

    You can try to add prefix partial before class LoginPageViewModel and declare loginmodel starting with small letter,thenLoginmodel property will be generated for you by MVVM Toolkit.

    You can refer to the following code:

    public  partial class LoginPageViewModel:ObservableObject 
    {
        [ObservableProperty]
        LoginModel loginmodel;
    
    
        public LoginPageViewModel() {
    
            //LoginModel login = new();
    
            Loginmodel = new LoginModel();
        }
    
        public  void Test() {
            if ((string.IsNullOrEmpty(Loginmodel.Username)) || (string.IsNullOrWhiteSpace(Loginmodel.Username)))
            {
                //IsBusy = false;
                Loginmodel.Validusername = false;
    
                return;
            }
        }
    }
    

    Update

    now the if statement is working but when I set Loginmodel.Validusername = false; the view is not updating with this new value.

    If you want the UI update automatically once changing the value of model Loginmodel, you also need to implement interface INotifyPropertyChanged for class LoginModel.

    Please refer to the following code:

    public  class LoginModel: INotifyPropertyChanged 
        {
            public string Failmessage { get; set; } = string.Empty;
    
            public string Username { get; set; } = string.Empty;
    
    
            public string Password { get; set; } = string.Empty;
    
            // public bool Validusername { get; set; } = true;
    
            // modify code as follows
            private bool _validusername;
            public bool Validusername
            {
                get => _validusername;
                set
                {
                    SetProperty(ref _validusername, value);
                }
            }
    
    
            public bool Vaildpassword { get; set; } = true;
    
    
            bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
            {
                if (Object.Equals(storage, value))
                    return false;
    
                storage = value;
                OnPropertyChanged(propertyName);
                return true;
            }
    
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }