【Win10】文件拖放打开
原文:
【Win10】文件拖放打开
在 Windows 10 中,通用应用程序在桌面环境下是支持从资源管理器拖放文件打开的。 这篇博文将演示拖放图片或文本文件,并在程序中打开显示。 前台 XAML:<Page x:Class="DropDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:DropDemo" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" AllowDrop="True" DragOver="Grid_DragOver" Drop="Grid_Drop"> <Image x:Name="img" Visibility="Collapsed" /> <TextBlock x:Name="txt" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" /> </Grid> </Page> 需要注意的是,能够接受拖放的控件的 Background 属性必须不能为 null,例如上面代码中,如果我们将 Grid 的 Background 属性去掉的话,则拖放就不起作用了。假如要实现一个拖放区域的话,那么我们可以将控件的 Background 属性设置为 Transparent,即为透明。这一点跟处理透明控件的点击事件是类似的。 后台代码:DragOver 方法:private void Grid_DragOver(object sender,DragEventArgs e) { e.AcceptedOperation = DataPackageOperation.Copy; // 设置拖放时显示的文字。 e.DragUIOverride.Caption = "拖放打开"; // 是否显示拖放时的文字。默认为 true。 // e.DragUIOverride.IsCaptionVisible = false; // 是否显示文件预览内容,一般为文件图标。默认为 true。 // e.DragUIOverride.IsContentVisible = false; // Caption 前面的图标是否显示。默认为 true。 // e.DragUIOverride.IsGlyphVisible = false; e.DragUIOverride.SetContentFromBitmapImage(new BitmapImage(new Uri("ms-appx:///Assets/dropContent.jpg"))); e.Handled = true; } 1、Caption 属性如果没设置的话,则会根据 AcceptedOperation 的值来显示,例如 Copy 的话显示复制,Move 的话,显示移动。 2、IsGlyphVisible 属性true: false: 可以明确看见文本前面的图标是否显示。 3、SetContentFromBitmapImage 方法这个方法可以传入一个图片,拖放的时候就可以显示图片了,例如我上面设置了一幅金馆长。如果没指定的话,则会显示文件的图标。 Drop 方法:private async void Grid_Drop(object sender,DragEventArgs e) { var defer = e.GetDeferral(); try { DataPackageView dataView = e.DataView; // 拖放类型为文件存储。 if (dataView.Contains(StandardDataFormats.StorageItems)) { var files = await dataView.GetStorageItemsAsync(); var file = files.OfType<StorageFile>().First(); if (file.FileType == ".png" || file.FileType == ".jpg") { // 拖放的是图片文件。 BitmapImage bitmap = new BitmapImage(); await bitmap.SetSourceAsync(await file.OpenAsync(FileAccessMode.Read)); img.Source = bitmap; img.Visibility = Visibility.Visible; txt.Visibility = Visibility.Collapsed; } else if (file.FileType == ".txt") { // 拖放的是文本文件。 string text = await FileIO.ReadTextAsync(file); txt.Text = text; img.Visibility = Visibility.Collapsed; txt.Visibility = Visibility.Visible; } } } finally { defer.Complete(); } } 由于 DragEventArgs 对象有 GetDeferral 方法,并且我们使用到异步方法,所以要先获取 defer 对象,异步方法完成后再调用 defer 对象的 Complete 方法。这点跟后台任务是类似的。 效果: Demo 下载:http://download.csdn.net/detail/h82258652/8838711 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用Windows Server AppFabric缓存的分布式锁服务
- windows-service – 在LocalService帐户下运行的Win Servic
- Celery ValueError: not enough values to unpack (expecte
- 使用PowerShell模仿控制向导的委派
- Window10上安装Redis及其客户端
- windows-phone-7 – Windows Phone 7 App栏,全球活动
- win-universal-app – Windows 10的应用内存限制是什么?
- Windows是否会回收堆栈空间?
- Microsoft office 2019 正式版镜像下载
- 配置 – .net核心,n层应用程序,服务层应该依赖于Microsoft.
- windows – vs 2010:错误LNK2028:未解析的令牌
- windows – 为什么我的WINVER 4 .exe在W98SE下运
- 图标 – 在Windows资源管理器中显示exe的图标时,
- windows – 带有嵌入密码的RDP文件要求输入密码
- 汇编 – 如何在DOS中以不同颜色编写文本?
- azure – 尝试通过Microsoft Virtual Machine Co
- windows – win32编程中的顶级窗口究竟是什么?
- windows-server-2003 – 是否可以将DOS PC连接到
- 如何在Windows应用程序中的VB.NET项目中使用C#类
- 在Windows服务模式下运行GUI应用程序