简体   繁体   English

在MVVM Windows Phone 8.1中传播PropertyChanged

[英]Propagate PropertyChanged in MVVM Windows Phone 8.1

I have a class: 我有一堂课:

public class Collection : INotifyPropertyChanged
{
    public Collection()
    {
        this.tracks = new ObservableCollection<Track>();
    }

    public void addTrack(Track track)
    {
        if (this.tracks.Contains(track)) return;

        this.tracks.Add(track);
        RaisePropertyChanged("tracks");
    }

    public ObservableCollection<Track> tracks
    {
        get
        {
            return _tracks;
        }
        set
        {
            if (_tracks != value)
            {
                _tracks = value;
                RaisePropertyChanged("tracks");
            }
        }
    }
    private ObservableCollection<Track> _tracks;

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

And a ViewModel: 还有一个ViewModel:

public class ScanningPageViewModel : ViewModelBase
{
    private INavigationService navigationService;

    public ScanningPageViewModel(INavigationService navigationService)
    {
        App.Collection.PropertyChanged += Collection_PropertyChanged;

        this.navigationService = navigationService;
        this.LoadData();
    }

    private async void LoadData()
    {
        await CollectionService.PopulateCollection();
        this.navigationService.NavigateTo("MainPage");
    }

    public int CollectionCount
    {
        get
        {
            return App.Collection.tracks.Count;
        }
    }

    public void Collection_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        RaisePropertyChanged("CollectionCount");
    }
}

Basically I want to raise a property changed for "CollectionCount" each time my Collection changes. 基本上,我想每次更改我的Collection时都为“ CollectionCount”更改一个属性。

I would have thought that 我本以为

App.Collection.PropertyChanged += Collection_PropertyChanged;

would do the trick but the method is not getting called (the RaisePropertyChanged in the Collection class is!)... 可以解决问题,但是方法没有被调用(Collection类中的RaisePropertyChanged是!)...

I'm new with MVVM so I might miss something obvious here but I'm not seeing it. 我是MVVM的新手,所以我在这里可能会错过一些显而易见的事情,但我没有看到。

You are not getting the PropertyChanged event because it's called too early. 您没有收到PropertyChanged事件,因为该事件为时过早。 It's called when the Collection is constructed. 在构造Collection时调用它。 You dont want to know when the whole collection instance has changed but rather when the items in the collections have changed. 您不想知道整个集合实例何时更改,而是想知道集合中的项目何时更改。 In this case use this event 在这种情况下,请使用此事件

Collection.Tracks.CollectionChanged += OnTracksCollectionChanged; Collection.Tracks.CollectionChanged + = OnTracksCollectionChanged;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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