private void mediaState_NrItemsInStateChanged(object sender, EventArgs e)
        {
            MediaStateCollectionView.EnterReadLock();
            try
            {
                NrPages = MediaStateCollectionView.Count();

                MediaItem selectedItem;

                int index = MediaStateCollectionView.getSelectedItem_RLock(out selectedItem);

                if (index != -1 && (index + 1) != CurrentPage)
                {
                    CurrentPage = index + 1;
                }
            }
            finally
            {
                MediaStateCollectionView.ExitReadLock();
            }
        }
        void initialize(IEventAggregator eventAggregator)
        {
            IsVisible = false;
            IsEnabled = true;

            NrPages         = 0;
            CurrentPage     = null;
            IsPagingEnabled = false;

            EventAggregator = eventAggregator;

            NextPageCommand = new Command(() =>
            {
                MediaStateCollectionView.EnterWriteLock();
                try
                {
                    int nrItems = MediaStateCollectionView.Count();

                    if (nrItems > 0)
                    {
                        MediaItem selectedItem;

                        int index = MediaStateCollectionView.getSelectedItem_RLock(out selectedItem);

                        if (selectedItem != null)
                        {
                            if (index + 1 < nrItems)
                            {
                                MediaStateCollectionView.selectExclusive_WLock(MediaStateCollectionView.ElementAt(index + 1).Item);
                            }
                        }
                    }
                }
                finally
                {
                    MediaStateCollectionView.ExitWriteLock();
                }
            });

            PrevPageCommand = new Command(() =>
            {
                MediaStateCollectionView.EnterWriteLock();
                try
                {
                    int nrItems = MediaStateCollectionView.Count();

                    if (nrItems > 0)
                    {
                        MediaItem selectedItem;

                        int index = MediaStateCollectionView.getSelectedItem_RLock(out selectedItem);

                        if (selectedItem != null)
                        {
                            if (index - 1 >= 0)
                            {
                                MediaStateCollectionView.selectExclusive_WLock(MediaStateCollectionView.ElementAt(index - 1).Item);
                            }
                        }
                    }
                }
                finally
                {
                    MediaStateCollectionView.ExitWriteLock();
                }
            });

            FirstPageCommand = new Command(() =>
            {
                MediaStateCollectionView.EnterWriteLock();
                try
                {
                    if (MediaStateCollectionView.Count() > 0)
                    {
                        MediaStateCollectionView.selectExclusive_WLock(MediaStateCollectionView.ElementAt(0).Item);
                    }
                }
                finally
                {
                    MediaStateCollectionView.ExitWriteLock();
                }
            });

            LastPageCommand = new Command(() =>
            {
                MediaStateCollectionView.EnterWriteLock();
                try
                {
                    if (MediaStateCollectionView.Count() > 0)
                    {
                        int lastIndex = MediaStateCollectionView.Count() - 1;

                        MediaStateCollectionView.selectExclusive_WLock(MediaStateCollectionView.ElementAt(lastIndex).Item);
                    }
                }
                finally
                {
                    MediaStateCollectionView.ExitWriteLock();
                }
            });

            BrowseLocationCommand = new Command <SelectableMediaItem>((selectableItem) =>
            {
                MediaItem item = selectableItem.Item;

                String location = FileUtils.getPathWithoutFileName(item.Location);

                EventAggregator.GetEvent <MediaBrowserPathChangedEvent>().Publish(location);
            });

            OpenLocationCommand = new Command <SelectableMediaItem>((selectableItem) =>
            {
                MediaItem item = selectableItem.Item;

                String location = FileUtils.getPathWithoutFileName(item.Location);

                Process.Start(location);
            });

            DeleteCommand = new Command <SelectableMediaItem>((selectableItem) =>
            {
                if (MessageBox.Show("Delete:\n\n" + selectableItem.Item.Location, "Delete file", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    List <MediaFileItem> item = new List <MediaFileItem>();
                    item.Add(selectableItem.Item as MediaFileItem);
                    CancellationTokenSource tokenSource = new CancellationTokenSource();

                    (MediaStateCollectionView.MediaState as MediaFileState).delete(item, tokenSource.Token);
                }
            });


            MediaStateCollectionView.NrItemsInStateChanged += mediaState_NrItemsInStateChanged;
            MediaStateCollectionView.ItemResorted          += mediaState_ItemResorted;
        }