Drag drop now supported in Universal apps!

When I am working on my desktop i am using regullary drag and drop. When i want to send an attachment to someone by email i simply drag it from the file explorer to the email. Or if want to send it with lync, drag a file from the fileexplorer to lync. In windows Store apps it wasn't possible to drag file to them. I found it pretty annoying to use the file picker. It required a lot of clicks to come at the location of the right file, to select the right files again. With Windows 10 universal apps also can be a drop target!

How to get drag drop in your application?

Well lets start with drop from explorer:

First need to enable your UI elements to be a drop target. To do this you need to implement 3 events: DragEnter, DragOver and Drop and set AllowDtop to true. This can look like this:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
          AllowDrop="True" DragEnter="Grid_DragEnter" DragOver="Grid_DragEnter" Drop="Grid_Drop">
        <TextBlock x:Name="DropFromExplorerText" Text="Drop here a file" VerticalAlignment="Center"  HorizontalAlignment="Center" Foreground="Black"/>
    </Grid>
</Page>

In code behind it's very easy too:

private void Grid_DragEnter(object sender, DragEventArgs e)
{
   e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}

I use this handler for DragEnter and DragOver. In realworld application's is most like you don't want to accept everything but first want to see what kind of file is dropped. You can inspect this in the e parameter.

The second method you need it the real drop handler. Process the data that is dropped on your application:

private async void Grid_Drop(object sender, DragEventArgs e)
{
    var d = e.GetDeferral();
    var files = await e.DataView.GetStorageItemsAsync();
    DropFromExplorerText.Text = files.First().Path;
    d.Complete();
}

First you need to get the deferral. Let the OS know you are going to process the incoming file. At the second like you can get the dropped StorageItems. then process and finally let the OS know you completed the action by calling Complete method of the Deferral.

As you can see it's very easy to start with DragDrop operations in your Windows 10 app. It can be real productivity boosters for desktop users!

  •   @JenMsft never mind; i found it by going to the old control panel; made a feedback item for it https://t.co/WmouYyriYV
  •   @peterskillman need help testing? :)
  •   @JenMsft i had to reset windows (recovery in the settings app) on my surface book 2 in order to be able to get a fi… https://t.co/0oTMC6nvpb
  •   RT @WBuzzer: Microsoft and Amazon Finally Launch Cortana and Alexa Integration https://t.co/8yJ1ijZRoU https://t.co/K24wtKFkSA
  •   @tfsbuck @thomsen_digital @cschleiden @matthewisabel +1 extension builders need guidiness for this. and what is the… https://t.co/3Ecj7tEkPG
  •   RT @rajen_k: Visual Studio 2017 version 15.8 is now available for download! Check out the details at https://t.co/G23ibMjjfY #VisualStudio
  •   @ingnl laatste windows 10; en ook op laatste insider build geprobeerd maar niet veel beter; Dus terug naar de vraag… https://t.co/zK70bdkiaM
  •   @Rabobank doe ik het voor jullie; gister de website omgezet :)
  •   @Rabobank nee gewoon voor mijn laptop
  •   @Rabobank een app? of zal ik mijn versie maar publiseren? https://t.co/ooJT7dJFQP