Esempio n. 1
0
        private async void OnPlayItemsListViewDrop(object sender, DragEventArgs e)
        {
            IEnumerable <MusicPlaybackItem> addItems = null;

            if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                var items = await e.DataView.GetStorageItemsAsync();

                addItems = await FileOpen.GetPlaybackItemsFromFilesAsync(
                    await FileOpen.GetAllFiles(items));
            }
            else if (e.DataView.Contains(StandardDataFormats.Text))
            {
                var items = DragHelper.Get(await e.DataView.GetTextAsync());

                if (items is Playlist)
                {
                    var list = items as Playlist;
                    addItems =
                        from i
                        in list.Items
                        select MusicPlaybackItem.CreateFromMediaFile(i.ToMediaFile());
                }
                else if (items is PlaylistItem)
                {
                    addItems = new MusicPlaybackItem[]
                    {
                        MusicPlaybackItem.CreateFromMediaFile(
                            (items as PlaylistItem).ToMediaFile())
                    };
                }
                else if (items is IEnumerable <DbMediaFile> )
                {
                    addItems =
                        from i
                        in items as IEnumerable <DbMediaFile>
                        select MusicPlaybackItem.CreateFromMediaFile(i);
                }
                else if (items is DbMediaFile)
                {
                    addItems = new MusicPlaybackItem[]
                    {
                        MusicPlaybackItem.CreateFromMediaFile(items as DbMediaFile)
                    };
                }
            }
            if (addItems != null)
            {
                var targetListView = sender as ListView;

                if (targetListView == null)
                {
                    return;
                }
                targetListView.Background = null;
                Border border = VisualTreeHelper.GetChild(targetListView, 0) as Border;

                ScrollViewer scrollViewer      = border.Child as ScrollViewer;
                var          droppedPosition   = e.GetPosition(targetListView).Y + scrollViewer.VerticalOffset;
                var          itemsSource       = targetListView.ItemsSource as IList;
                var          highWaterMark     = 3d; // 3px of padding
                var          dropIndex         = 0;
                var          foundDropLocation = false;

                for (int i = 0; i < itemsSource.Count && !foundDropLocation; i++)
                {
                    var itemContainer = (ListViewItem)targetListView.ContainerFromIndex(i);

                    if (itemContainer != null)
                    {
                        highWaterMark = highWaterMark + itemContainer.ActualHeight;
                    }

                    if (droppedPosition <= highWaterMark)
                    {
                        dropIndex         = i;
                        foundDropLocation = true;
                    }
                }

                if (foundDropLocation)
                {
                    await Core.PlaybackControl.Instance.AddFile(addItems, dropIndex);
                }
                else
                {
                    await Core.PlaybackControl.Instance.AddFile(addItems);
                }
            }
        }