locked
Implementing INotifyPropertyChanged in xamarin.forms RRS feed

  • Question

  • User390048 posted

    Hi everyone, so I have a model class and I want to creat a ViewModel so that I can track the changes that happen in one of my Model class arguments. what should I do ? this what I've done so far Model Class :

    public class Objets
        {
        public string Designation { get; set; }
        public string Description { get; set; }
        public float Prix { get; set; }
        public int nbr_objet { get; set; }
        public Objets(string Designation, string Description, float Prix, int nbr_objet)
        {
            this.Designation = Designation;
            this.Description = Description;
            this.Prix = Prix;
            this.nbr_objet= nbr_objet;
        }
        }
    

    ModelViewBase where I have the problem obviously

    class ViewModelBase : INotifyPropertyChanged
        {
        public Objets ObjetVM { get; set; }
        public int nbr_objet 
        {
            get { return ObjetVM.nbr_objet; }
            set
            {
                ObjetVM.nbr_objet = value;
                OnPropertyChanged(nameof(ObjetVM.nbr_objet));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string propertyname)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
    }
    

    And this is my .XAML.CS page

        public MenuPage()
        {
            InitializeComponent();
            this.BindingContext = new ViewModelBase();
        }
    
    Tuesday, October 29, 2019 11:55 PM

All replies

  • User390048 posted

    Can anyone help me out ??

    Wednesday, October 30, 2019 11:33 AM
  • User390048 posted

    is this so hard to do for everyone or no one actually wants to help ?

    Thursday, October 31, 2019 11:28 AM
  • User89714 posted

    @OdShad said: is this so hard to do for everyone or no one actually wants to help ?

    You haven't actually said what problem you are hitting, or what you are stuck on.

    However, a few pointers:

    Subclass ViewModelBase: public class SampleViewModel : ViewModelBase { }

    Then move the following from ViewModelBase to SampleViewModel public Objets ObjetVM { get; set; } public int nbr_objet { get { return ObjetVM.nbr_objet; } set { ObjetVM.nbr_objet = value; OnPropertyChanged(nameof(ObjetVM.nbr_objet)); } }

    Then change this.BindingContext = new ViewModelBase();

    to this.BindingContext = new SampleViewModel();

    You also need to set the ObjetVM property of SampleViewModel, otherwise you will have a default set of properties each time MenuPage is created.

    Friday, November 1, 2019 12:10 PM
  • User365353 posted

    First, when you creating new project there is a basic sample how to use MVVM. Second, go to Xamarin University and upgrade your skills there. https://docs.microsoft.com/en-us/learn/modules/design-a-mvvm-viewmodel-for-xamarin-forms/ https://docs.microsoft.com/en-us/learn/modules/separate-your-ui-and-code-with-xamarin-forms-data-binding/

    https://docs.microsoft.com/ru-ru/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

    Sunday, November 3, 2019 9:57 PM