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

问题描述 投票:0回答:2

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

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

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

我不知道该怎么做,但我想知道是否有任何“掉落”事件或类似的事件我可以处理。

谢谢!

c# wpf drag-and-drop
2个回答
5
投票

这个片段:

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 应该可以。 。非常好的和简单的解决方案。


1
投票

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)
     {
     }
© www.soinside.com 2019 - 2024. All rights reserved.