WPF:将虚拟文件拖放到 Windows 资源管理器中

时间:2023-04-26
本文介绍了WPF:将虚拟文件拖放到 Windows 资源管理器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个类似于 dropbox 的应用程序,并在 WPF 列表视图中显示远程文件.我想将这些元素拖放到 Windows 资源管理器中.我见过这样的代码:

I'm developing an application similar to dropbox and i show the remote files on a WPF listview. I want to drag those elements and drop it into windows explorer. I've seen code like this:

var dataObject = new DataObject(DataFormats.FileDrop, files.ToArray());
dataObject.SetData(DataFormats.StringFormat, dataObject);
DoDragDrop(dataObject, DragDropEffects.Copy);

但正如您可能认为的那样,这些文件还没有在本地系统中,在复制它们之前,我需要连接到服务器,下载并解压缩文件.就像 ftp 客户端一样.

But as you may think, those file are not at the local system yet, before copiying them I need to connect to server, donwload and unzip the files. Like a ftp client does.

我不知道怎么做,但我想知道是否有任何drop"事件或类似的我可以处理.

I dont how to do it but i was wondering if there is any "drop" event or similiar that i can handle.

谢谢!

推荐答案

这段代码:

var virtualFileDataObject = new VirtualFileDataObject(
                // BeginInvoke ensures UI operations happen on the right thread
                (vfdo) => Dispatcher.BeginInvoke((Action)(() => BusyScreen.Visibility = Visibility.Visible)),
                (vfdo) => Dispatcher.BeginInvoke((Action)(() => BusyScreen.Visibility = Visibility.Collapsed)));

            // Provide a virtual file (downloaded on demand), its URL, and descriptive text
            virtualFileDataObject.SetData(new VirtualFileDataObject.FileDescriptor[]
            {
                new VirtualFileDataObject.FileDescriptor
                {
                    Name = "DelaysBlog.xml",
                    StreamContents = stream =>
                        {
                            using(var webClient = new WebClient())
                            {
                                var data = webClient.DownloadData("http://blogs.msdn.com/delay/rss.xml");
                                stream.Write(data, 0, data.Length);
                            }
                        }
                },
            });
            virtualFileDataObject.SetData(
                (short)(DataFormats.GetDataFormat(CFSTR_INETURLA).Id),
                Encoding.Default.GetBytes("http://blogs.msdn.com/delay/rss.xml"));
            virtualFileDataObject.SetData(
                (short)(DataFormats.GetDataFormat(DataFormats.Text).Id),
                Encoding.Default.GetBytes("[The RSS feed for Delay's Blog]"));

            DoDragDropOrClipboardSetDataObject(e.ChangedButton, TextUrl, virtualFileDataObject, DragDropEffects.Copy);

使用类 linked 应该可以工作..非常好的和简单的解决方案.

Using the class linked should work. . Very nice and easy solution.

这篇关于WPF:将虚拟文件拖放到 Windows 资源管理器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:DoDragDrop 禁用 MouseMove 事件 下一篇:C# 在 Canvas 中拖放图像

相关文章