触发OnPropertyChanged后,CollectionView绑定数据不会更新

时间:2020-10-31 20:27:16

标签: c# xaml xamarin.forms data-binding

我有一个CollectionView,它绑定到ObservableCollection<Announce> Announces ,当页面出现时,该视图正确地填充了数据。然后,我发送一个发布请求,以创建一个新的公告,调用OnPropertyChanged方法,更新Announcesannounces中的数据,但不调用getter并收集CollectionView中的数据没有更新。应该在OnPropertyChanged之后自动调用getter吗?在我的代码中缺少哪些内容,这些内容无法更新UI?

AnnouncesService是一个简单的服务,可从http请求返回Announce的ObservableCollections。 这是我的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));
        }

    }

这是我的具有绑定功能的XAML

 <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>

和一个TabbedPage,它从AnnouncesViewModel

调用AddAnnounce
 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);
        }
}

1 个答案:

答案 0 :(得分:0)

所以问题是我正在使用AnnouncesViewModel的2个不同实例。一种用XAML调用,另一种用cs代码调用AddAnnounce。 为了解决这个问题,我从XAML代码中删除了该部分

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

在我的ContentPage中添加了x:Name = "page"并在TabbedPage1构造函数中分配了BindingContext:

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

我还在AnnouncesViewModel中使用Add方法向我的Announces ObservableCollection添加内容。