简体   繁体   中英

CollectionView binding data doesn't update after OnPropertyChanged is triggered

I have a CollectionView with a binding to ObservableCollection<Announce> Announces that is filled correctly with data when the page appears. Then I send a Post request creating a new Announce, the OnPropertyChanged method is called, data in Announces aswell as in announces is updated but the getter isn't called and data in the CollectionView isn't updated. Should the getter be called automatically after OnPropertyChanged ? What am I missing in my code that doesn't update the UI?

The AnnouncesService is a simple service that returns ObservableCollections of Announce from http requests. Here's my AnnouncesViewModel:

 public class AnnouncesViewModel : INotifyPropertyChanged
    {
        ObservableCollection<Announce> announces;
        public ObservableCollection<Announce> Announces 
        {
            get => announces;
            set { announces = value; OnPropertyChanged("Announces"); } 
        }
        public AnnouncesService service;
        public event PropertyChangedEventHandler PropertyChanged;

        public AnnouncesViewModel()
        {
            service = new AnnouncesService();
            Announces = new ObservableCollection<Announce>();
            getAnnounces();
        }

        public async Task addAnnounce(Announce announce)
        {
            Announces = await service.PostAnnounceAsync(announce); //same as using getAnnounces again            
        }
       
        public async void getAnnounces()
        {
            Announces = await service.GetAnnouncesAsync();            
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }

Here's my XAML with binding

 <ContentPage Title="">
        <ContentPage.BindingContext>
                <vm:AnnouncesViewModel/>
            </ContentPage.BindingContext>
        <ContentPage.Content>

            <CollectionView ItemsSource="{Binding Announces}"
                            SelectionMode="Single"
                            SelectionChanged="CollectionView_SelectionChanged">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding location}"/>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </ContentPage.Content>
    </ContentPage>

and a TabbedPage that calls AddAnnounce from the AnnouncesViewModel

 public partial class TabbedPage1 : TabbedPage
    {
        private AnnouncesService service;
        private AnnouncesViewModel viewModel;
        public TabbedPage1()
        {
            InitializeComponent();
            service = new AnnouncesService();
            viewModel = new AnnouncesViewModel();
        }

        private async void AddAnnounce(object sender, EventArgs e)
        {
            Announce announce = new Announce(localtion_entry.Text,sport_entry.Text,1);
            await viewModel.addAnnounce(announce);
        }
}

So the issue was I was using 2 different instances of AnnouncesViewModel. One in XAML and one in cs code to call AddAnnounce . To fix this I removed this part from my XAML code

<ContentPage.BindingContext>
                <vm:AnnouncesViewModel/>
</ContentPage.BindingContext>

added x:Name = "page" to my ContentPage and assigned the BindingContext in TabbedPage1 constructor:

private AnnouncesViewModel viewModel;
public TabbedPage1()
{
    InitializeComponent();
    viewModel = new AnnouncesViewModel();
    page.BindingContext = viewModel;
}

I also use Add method in AnnouncesViewModel to add content to my Announces ObservableCollection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM