简体   繁体   English

您如何为视频而不是图像实现拖放?

[英]How do you implement Drag and Drop for video instead of image?

I cant seem to implement a Drag and Drop Function for video files (.mp4), only images.我似乎无法为视频文件(.mp4)实现拖放 Function,只有图像。 Everytime I try to drag a video, the program crashed.每次我尝试拖动视频时,程序都会崩溃。 Here is the drop canvas I had:这是我的下降 canvas:

private async void mainCanvas_Drop(object sender, DragEventArgs e)
    {
        Image img = new Image();
        img.Width = 200;
        img.Height = 150;
        BitmapImage bm = new BitmapImage();
        if (e.DataView.Contains(StandardDataFormats.StorageItems))
        {
            var storageItems = await e.DataView.GetStorageItemsAsync();
            foreach (StorageFile file in storageItems)
            {
                var stream = await
               file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                await bm.SetSourceAsync(stream);
                img.Source = bm;
            }
            img.RenderTransform = new CompositeTransform();
            img.ManipulationMode = ManipulationModes.All;
            img.ManipulationStarted += mPlayer_ManipulationStarted;
            img.ManipulationCompleted += mPlayer_ManipulationCompleted;
            img.ManipulationDelta += mPlayer_ManipulationDelta;
            mainCanvas.Children.Add(img);
            Canvas.SetLeft(img, e.GetPosition(mainCanvas).X);
            Canvas.SetTop(img, e.GetPosition(mainCanvas).Y);
        }

    }

You cant set a video as a source to BitmapImage .您不能将视频设置为BitmapImage的源。

BitmapImage bm = new BitmapImage(); BitmapImage bm = new BitmapImage(); await bm.SetSourceAsync(stream);等待 bm.SetSourceAsync(stream);

You need to use MediaPlayerElement to play a video.您需要使用MediaPlayerElement来播放视频。 Have a look at the example .看看这个例子

Edit编辑

I have to delete the drag and drop for image.我必须删除图像的拖放。 Is there a way to detect whether the item Im dropping is image or video?有没有办法检测我丢弃的项目是图像还是视频?

    private async void mainGrid_Drop(object sender, DragEventArgs e)
    {
        Image img = new Image();
        img.Width = 200;
        img.Height = 150;
        BitmapImage bm = new BitmapImage();
        MediaPlayerElement mediaPlayerElement = new MediaPlayerElement();
        if (e.DataView.Contains(StandardDataFormats.StorageItems))
        {
            var storageItems = await e.DataView.GetStorageItemsAsync();
            foreach (StorageFile file in storageItems)
            {
                if (file.FileType == ".mp4")
                {
                    mediaPlayerElement.Source = MediaSource.CreateFromStorageFile(file);
                    mainGrid.Children.Add(mediaPlayerElement);
                }
                else
                {
                    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                    await bm.SetSourceAsync(stream);
                    img.Source = bm;
                    mainGrid.Children.Add(img);
                }
            }
        }
    }

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

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