[英]WPF: Drag and drop virtual files into Windows explorer
我正在開發一個類似於dropbox的應用程序,我在WPF列表視圖中顯示遠程文件。 我想拖動這些元素並將其放入Windows資源管理器中。 我見過這樣的代碼:
var dataObject = new DataObject(DataFormats.FileDrop, files.ToArray());
dataObject.SetData(DataFormats.StringFormat, dataObject);
DoDragDrop(dataObject, DragDropEffects.Copy);
但是你可能認為,那些文件還沒有在本地系統上,在復制它們之前我需要連接到服務器,下載並解壓縮文件。 像ftp客戶端一樣。
我不知道怎么做,但我想知道是否有任何“掉落”事件或類似我能處理。
謝謝!
這個片段:
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);
使用鏈接的類應該工作。 。 非常好,簡單的解決方案。
http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!190.entry http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!199.entry http://pavanpodila.spaces。 live.com/blog/cns!9C9E888164859398!225.entry
看到這一系列文章。 這應該可以幫助您入門。
編輯:看看這個dragsourceadvisor的實現
internal class ImagesViewPanelDragSourceAdvisor : IDragSourceAdvisor
{
private FrameworkElement _dragSource;
public DependencyObject DragSource
{
get
{
return _dragSource;
}
set
{
_dragSource = value as FrameworkElement;
}
}
public DependencyObject DragObject { get; set; }
public DragDropEffects GetDragDropEffects()
{
DragDropEffects effects = DragDropEffects.None;
FrameworkElement frameworkObj = DragObject as FrameworkElement;
if (frameworkObj != null && frameworkObj.DataContext is ImageViewModel)
{
effects = DragDropEffects.Copy;
}
return effects;
}
public IDataObject GetDragDataObject()
{
Debug.Assert(GetDragDropEffects() != DragDropEffects.None);
ImagesViewModel imagesVM = (FrameworkElement)DragSource).DataContext as ImagesViewModel;
StringCollection fileList = new StringCollection();
foreach (ImageViewModel imageVM in imagesVM.Items.Where(imageVM => imageVM.IsSelected))
{
fileList.Add(imageVM.ImagePath);
}
Debug.Assert(fileList.Count > 0);
DataObject dataObj = new DataObject();
dataObj.SetFileDropList(fileList);
return dataObj;
}
public void FinishDrag(DragDropEffects finalEffect)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.