简体   繁体   English

WPF 拖放至外部应用程序 (Outlook)

[英]WPF Drag & Drop to External Application (Outlook)

I am attempting to implement some additional Drag & Drop functionality to my WPF application.我正在尝试为我的 WPF 应用程序实现一些额外的拖放功能。 I have Drag & Drop working when the source and target are within my application, but I need to be able to drag data from my application to Outlook (Desktop App) and have it be uploaded as a file upon the drop .当源和目标在我的应用程序中时,我有拖放工作,但我需要能够将数据从我的应用程序拖到 Outlook(桌面应用程序),并在放置时将其作为文件上传

I am familiar with the standard DragDrop.DoDragDrop() method, and the events associated with typical drag-drops.我熟悉标准的 DragDrop.DoDragDrop() 方法,以及与典型拖放相关的事件。

Current Markup:当前标记:

MouseLeftButtonDown="Data_MouseLeftButtonDown"

Current Event Handler Code:当前事件处理程序代码:

/// Getting the data that we want to export (For example only)
private void Data_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Write data to a file (For example only)
    var fileToExport = File.ReadAllLines("C:\\test.txt");

    if (e.OriginalSource is Grid grid && grid.DataContext is CustomDataObject cdo)
    {
        DragDrop.DoDragDrop(ControlGoesHere, fileToExport, DragDropEffects.Copy);
    }
}

I have tried most, if not all, of the other SO solutions I researched, but I am still unable to drop this data into Outlook.我已经尝试了大部分(如果不是全部)我研究过的其他 SO 解决方案,但我仍然无法将这些数据放入 Outlook。 Additionally, I disabled "Just My Code" and enabled all CLR exceptions and saw this exception being thrown:此外,我禁用了“Just My Code”并启用了所有 CLR 异常,并看到抛出了这个异常:

System.Runtime.InteropServices.COMException: 'Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))'

It is my understanding that DoDragDrop constructs a DataObject based on the data you pass in, and that you can instantiate the DataObject manually before passing it in for more control, etc, but I haven't gotten any configuration working so far.据我了解,DoDragDrop 会根据您传入的数据构造一个 DataObject,并且您可以在传入 DataObject 之前手动实例化该 DataObject 以获得更多控制等,但到目前为止我还没有进行任何配置。

There are a few things I'm uncertain of有几件事我不确定

  • Does the data that gets 'exported' need to come from the source control? “导出”的数据是否需要来自源代码管理? (I successfully managed to export a string "Test" to Outlook that wasn't from the control. (我成功地将一个字符串“Test”导出到 Outlook 中,该字符串不是来自控件。
  • In order to have DoDragDrop return something, I've got to press ctrl or other keys, is this due to the setup of my current implemenation not handling the drop logic properly (by using QueryContinueDrag for example?为了让DoDragDrop返回一些东西,我必须按 ctrl 或其他键,这是因为我当前实现的设置没有正确处理放置逻辑(例如使用QueryContinueDrag

WPF, .NET Framework 4.6.1 WPF、.NET 框架 4.6.1

I can give more information as needed on request, just let me know.我可以根据要求提供更多信息,请告诉我。 Thanks in advance!提前致谢!

Outlook needs to know what it's receiving. Outlook 需要知道它正在接收什么。 To do that, you create a DataObject and give it a format type of FileDrop.为此,您需要创建一个 DataObject 并为其指定一个 FileDrop 格式类型。 FileDrop expects a string[] as the object value, so use a string[] to input your file PATH or paths if you have more than one. FileDrop 需要一个 string[] 作为对象值,因此如果您有多个文件路径或路径,请使用 string[] 输入您的文件 PATH 或路径。 do NOT try and read the whole file.不要尝试阅读整个文件。

string[] files = new string[1];
files[0] = ""; //fill this in with the file PATH
DataObject data = new DataObject(DataFormats.FileDrop, files);
DragDrop.DoDragDrop(listTest, data, DragDropEffects.Copy);

With this I was able to drag an item into the message area of an outlook e-mail and have it attach the file with the path I input.有了这个,我就可以将一个项目拖到 Outlook 电子邮件的消息区域中,并将其附加到带有我输入的路径的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM