[Win10] drag and drop files to open, win10 files to drag and drop

Source: Internet
Author: User

[Win10] drag and drop files to open, win10 files to drag and drop

In Windows 10, generic applications can be opened by dragging and dropping files from the resource manager in a desktop environment.

This blog post demonstrates drag-and-drop images or text files, and opens the display in the program.

Foreground 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>

Note that the Background attribute of controls that can be dragged and dropped must not be null. For example, in the code above, if we remove the Background attribute of Grid, drag and drop will not work. If you want to implement a drag-and-drop area, you can set the control's Background attribute to Transparent, that is, Transparent. This is similar to processing click events of transparent controls.

Background code: DragOver method:
Private void Grid_DragOver (object sender, DragEventArgs e) {e. AcceptedOperation = DataPackageOperation. Copy; // set the text displayed during drag and drop. E. DragUIOverride. Caption = "drag-and-drop open"; // whether to display the text during drag-and-drop. The default value is true. // E. DragUIOverride. IsCaptionVisible = false; // whether to display the file preview content, which is generally a file icon. The default value is true. // E. DragUIOverride. IsContentVisible = false; // whether the icon in front of Caption is displayed. The default value is true. // E. dragUIOverride. isGlyphVisible = false; e. dragUIOverride. setContentFromBitmapImage (new BitmapImage (new Uri ("ms-appx: // Assets/dropContent.jpg"); e. handled = true ;}
1. Caption attributes

If this parameter is not set, it is displayed based on the value of AcceptedOperation. For example, if Copy is used, Copy is displayed, and Move is displayed.

2. IsGlyphVisible attributes

True:

Private async void Grid_Drop (object sender, DragEventArgs e) {var defer = e. GetDeferral (); try {DataPackageView dataView = e. DataView; // The drag-and-drop type is file storage. If (dataView. contains (StandardDataFormats. storageItems) {var files = await dataView. getStorageItemsAsync (); var file = files. ofType <StorageFile> (). first (); if (file. fileType = ". png "| file. fileType = ". jpg ") {// drag and drop an image file. 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 ") {// drag and drop a text file. String text = await FileIO. readTextAsync (file); txt. text = text; img. visibility = Visibility. collapsed; txt. visibility = Visibility. visible ;}}finally {defer. complete ();}}

Because the DragEventArgs object has the GetDeferral method and we use the asynchronous method, we need to obtain the defer object first, and then call the Complete method of the defer object after the Asynchronous Method is completed. This is similar to background tasks.

Effect:

Demo download: http://download.csdn.net/detail/h82258652/8838711

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.