unable to update ui when observablecollection add item

candinho Silveira 1 Reputation point
2022-11-22T19:58:52.517+00:00

Hi everyone, i'n strugging to make may ui update when i add an item to my observablecollection, have tried with and without mvvmtoolkit and it doesn't work, i've set a break point to check if the collection get the new Medida, and id does. but the ui never get updated except if i close que page and open it again, i'n using Maui and CommunityToolkit.Mvvm
public partial class MedidasViewModel : ObservableObject, INotifyCollectionChanged

    {  
        public event NotifyCollectionChangedEventHandler CollectionChanged;  
          
        [ObservableProperty]  
        private ObservableCollection<Medida> _medidas;  
          
             
  
  
        public void GetAll()  
        {  
              
            using (AppDbContext context = new AppDbContext())  
            {  
             
                List<Medida> lista = context.Medida.ToList();  
  
  
                foreach (var item in lista)  
                {  
                    Medidas.Add(item);  
  
                }  
            }  
        }  
  
  
        public MedidasViewModel()  
        {  
            Medidas = new ObservableCollection<Medida>();  
             
            GetAll();  
             
  
        }  
  
        [ObservableProperty]  
        [NotifyPropertyChangedFor(nameof(DescricaoNovaMedida))]  
        string descricaoNova;  
  
        [ObservableProperty]  
        [NotifyPropertyChangedFor(nameof(SiglaNovaMedida))]  
        string siglaNova;  
  
        [ObservableProperty]  
        [NotifyPropertyChangedFor(nameof(ConvervorNovaMedida))]  
        decimal conversorNova;  
  
       
  
        public string DescricaoNovaMedida => descricaoNova;  
        public string SiglaNovaMedida => siglaNova;  
        public double ConvervorNovaMedida => Convert.ToDouble(conversorNova);  
  
          
        [RelayCommand]  
        void SalvarNovaMedidaVM()  
        {  
  
            Medida medida = new Medida  
            {  
                Descricao = DescricaoNovaMedida,  
                Sigla = SiglaNovaMedida,  
                Conversor = Convert.ToDecimal(ConvervorNovaMedida)  
            };  
            using (var context = new AppDbContext())  
            {  
                context.Medida.Add(medida);  
                context.SaveChanges();  
                int idNovaMedida = medida.Id;  
                Medidas.Add(medida); //here item is added succesfuly  
               // Medidas.CollectionChanged();  
                 
               Medidas.CollectionChanged += (sender, e) => CollectionChanged?.Invoke(this, e);  
                //Medidas.Clear(); tried that too, no luck  
                //GetAll();   
                  
            }  
  
  
        }  
          
    }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,208 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 38,911 Reputation points Microsoft Vendor
    2022-11-28T07:44:54.423+00:00

    Hello,

    i forgot to mention that i use popup to register a new Medida, then i moved the picker to the popup to test.

    This should be caused by the ViewModel being created twice in the main page and popup.

    Does this issue occur if you call the add method using the following singleton mode?

       private static readonly object padlock = new object();  
               private static MedidasViewModel instance;  
               public static MedidasViewModel Instace  
               {  
                   get  
                   {  
                       lock (padlock)  
                       {  
                           if (instance == null)  
                           {  
                               instance = new MedidasViewModel();  
                           }  
                           return instance;  
                       }  
                   }  
               }  
       public MedidasViewModel()  
               {  
                   Medidas = new ObservableCollection<Medida>();  
                  GetAll();  
                  instance = this;  
               }  
    

    // Class that calls pages and view models.

       public MedidasViewModel MedidasView { get; } = MedidasViewModel.Instace;  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.