WPF:将虚拟文件拖放到Windows资源管理器中 [英] WPF: Drag and drop virtual files into Windows explorer
问题描述
我正在开发类似于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\0"));
virtualFileDataObject.SetData(
(short)(DataFormats.GetDataFormat(DataFormats.Text).Id),
Encoding.Default.GetBytes("[The RSS feed for Delay's Blog]\0"));
DoDragDropOrClipboardSetDataObject(e.ChangedButton, TextUrl, virtualFileDataObject, DragDropEffects.Copy);
使用类 linked 应该工作。 。非常好的解决方案。
Using the class linked should work. . Very nice and easy solution.
这篇关于WPF:将虚拟文件拖放到Windows资源管理器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!