我试图从另一个 Windows 应用程序(EM 客户端、thunderbird 或 Outlook)拖到我的表单上。当电子邮件从其他应用程序拖到 windows explore 时,它会作为一个文件放置。如果用户拖动到我的应用程序上,我希望将文件内容作为文件流获取。
我能够在 UWP 应用程序中使用它,但我需要让它在 Windows 窗体应用程序中运行,以便它可以在 Windows 7 中运行。
我发现了很多相反的例子(从应用程序拖到窗口)。
使这如此烦人的事情是在 UWP 应用程序中很容易。这是我在 UWP 应用程序中的操作方法,结果是我在漫游文件夹中保存了一个名为“email.eml”的新文件:
XAML
<Grid AllowDrop="True" DragOver="Grid_DragOver" Drop="Grid_Drop"
Background="LightBlue" Margin="10,10,10,353">
<TextBlock>Drop anywhere in the blue area</TextBlock>
</Grid>
XAML文件
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Grid_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
}
private async void Grid_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
var storageFile = items[0] as StorageFile;
var reader = (await storageFile.OpenAsync(FileAccessMode.Read));
IBuffer result = new byte[reader.Size].AsBuffer();
var test = await reader.ReadAsync(result, result.Length,Windows.Storage.Streams.InputStreamOptions.None);
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync("email.eml",Windows.Storage.CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, test);
}
}
}
}
}
德玛西亚99
相关分类