【问题标题】:Drag and Drop file upload in Silverlight 4?在 Silverlight 4 中拖放文件上传?
【发布时间】:2016-02-19 18:39:49
【问题描述】:

我正在将 WinForms 应用程序重新编写到 Silverlight 中。 WinForms 应用程序中的一个用例允许用户将 TIFF 图像(传真)从 Outlook 直接拖到 WinForms 应用程序中的“将图像或传真附加到此案例”控件上。是否有允许相同功能的 Silverlight 4 控件?请务必认识到,将文件保存到本地文件系统,然后使用标准上传控件选择并上传文件将无法满足项目中概述的业务需求。

【问题讨论】:

    标签: silverlight drag-and-drop silverlight-4.0


    【解决方案1】:

    Silverlight 4 确实支持将文件从文件系统拖放到任何UIElement。看到这个blog

    但是,我不知道这是否适用于在 Outlook 中启动的拖动。我建议你从这个博客中获取示例并构建一个小测试应用程序,看看你是否可以拖动附件。

    当然,您仍然需要将 TIFF 解码为 Silverlight 可以使用的位图。

    【讨论】:

      【解决方案2】:

      (我的猜测是,由于 Silverlight 的“轻量级”性质,它对于大部分功能来说将是一个艰难的移植。您可能需要考虑使用 Click-once WPF 应用程序而不是 Silverlight,尤其是因为您已经依赖在丰富的已安装 Windows 应用程序上)。

      您需要在 Silverlight 用户控件的构造函数中执行类似的操作,

      this.Drop += new DragEventHandler(MainPage_Drop);
      

      然后添加一个方法

      void MainPage_Drop(object sender, DragEventArgs e)
      {
          IDataObject drop = e.Data;
      
          if (drop.GetDataPresent(System.Windows.DataFormats.FileDrop))
          {
              FileInfo[] files = (FileInfo[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
              foreach (FileInfo file in files)
              {
                // do something with each file here
              }
          }
      }
      

      然后——看看会发生什么。您需要使用库来添加 TIFF 支持,可能类似于 Stackoverflow 上的建议,here

      【讨论】:

        【解决方案3】:

        您可以在 silverlight 应用程序中从桌面拖放。我已经在我们的项目中实现了这一点。在 silverlight 项目属性中选中“需要提升的权限”,并使用 silverlight datagrid 的 drop 事件,可以在 silverlight datagrid 中处理从桌面拖放。

            private void DocumentsDrop(object sender, DragEventArgs e)
            {
                e.Handled = true;
        
                var point = e.GetPosition(null);
                var dataGridRow = ExtractDataGridRow(point);
                if(dataGridRow !=null)
                {.....
                 }
        
                var droppedItems = e.Data.GetData(DataFormats.FileDrop) as      FileInfo[];
                if (droppedItems != null)
                     {
                        var droppedDocumentsList = new List<FileInfo>();
        
                        foreach (var droppedItem in droppedItems)
                        {
                            if ((droppedItem.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                            {
                                var directory = new DirectoryInfo(droppedItem.FullName);
                                droppedDocumentsList.AddRange(directory.EnumerateFiles("*", SearchOption.AllDirectories));
                            }
                            else
                            {
                                droppedDocumentsList.Add(droppedItem);
                            }
                        }
        
                        if (droppedDocumentsList.Any())
                        {
                            ProcessFiles(droppedDocumentsList);
                        }
                        else
                        {
                            DisplayErrorMessage("The selected folder is empty.");
                        }
                    }
           }
        

        设置 AllowDrop =true;在用于数据网格的 xaml 中。从 DragEventArgs 中提取信息作为 FileInfo 对象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-15
          相关资源
          最近更新 更多