簡體   English   中英

觸發 OnPropertyChanged 后 CollectionView 綁定數據不更新

[英]CollectionView binding data doesn't update after OnPropertyChanged is triggered

我有一個綁定到ObservableCollection<Announce> Announces的 CollectionView,當頁面出現時,它會正確填充數據。 然后我發送一個 Post 請求,創建一個新的announces ,調用OnPropertyChanged方法,更新了Announcesannounces數據,但是getter 沒有被調用,CollectionView 中的數據也沒有更新。 是否應該在OnPropertyChanged之后自動調用 getter? 我在不更新 UI 的代碼中缺少什么?

公告服務是一個簡單的服務,它從 http 請求中返回公告的 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>

和從調用AddAnnounce一個TabbedPage 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);
        }
}

所以問題是我使用了兩個不同的AnnouncesViewModel 實例。 一種在 XAML 中,一種在 cs 代碼中用於調用AddAnnounce 為了解決這個問題,我從我的 XAML 代碼中刪除了這部分

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

x:Name = "page"到我的 ContentPage 並在 TabbedPage1 構造函數中分配了 BindingContext:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM