microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.35k stars 678 forks source link

WinUI 3 Desktop: Drag and drop file from Explorer to app window does not work #10119

Open Iomegan opened 2 days ago

Iomegan commented 2 days ago

Describe the bug

I have the following simple WinUI 3 Desktop app running on Window 11. Dragging a file from the Explorer into the window does not work. Grid_DragOver() is not being called. You just get the red cursor symbol as if drop is not allowed.

Strangely it works if I drag a file from the Recent items in the Start section of the Explorer. But all other places like the Desktop don't work.

Steps to reproduce the bug

MainWindow.xaml:

 <Grid AllowDrop="True" Drop="Grid_Drop" DragOver="Grid_DragOver" Background="LightGray">
        <TextBlock
            x:Name="FileNameTextBlock"
            Text="Drag a file here"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="20"
            FontWeight="Bold" />
    </Grid>
</Window>

MainWindow.xaml.cs:

 using Microsoft.UI.Xaml;
 using Microsoft.UI.Xaml.Controls;
 using Microsoft.UI.Xaml.Input;
 using System;
 using System.IO;
 using Windows.ApplicationModel.DataTransfer;
 using Windows.Storage;

namespace FileDragDropApp
{
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private async void Grid_Drop(object sender, DragEventArgs e)
        {
            // Check if the dropped data contains files
            if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                // Get the file(s) from the DataPackage
                var items = await e.DataView.GetStorageItemsAsync();
                if (items.Count > 0)
                {
                    // Display the first file's name
                    var storageFile = items[0] as StorageFile;
                    if (storageFile != null)
                    {
                        FileNameTextBlock.Text = $"File: {storageFile.Name}";
                    }
                }
            }
        }

        private void Grid_DragOver(object sender, DragEventArgs e)
        {
            // This is not being called
            e.AcceptedOperation = DataPackageOperation.Copy;
        }
    }
}

Expected behavior

Dragging files from the explorer onto a AllowDrop="True" enabled view should work.

Screenshots

This one works: Image

This does not: Image Image

NuGet package version

WinUI 3 - Windows App SDK 1.6.1: 1.6.240923002

Windows version

Windows Insider Build (xxxxx), Windows 11 (22H2): Build 22621

Additional context

Edition Windows 11 Pro Version 24H2 OS build 26100.2161 Experience Windows Feature Experience Pack 1000.26100.32.0

github-actions[bot] commented 2 days ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

castorix commented 1 day ago

I tried to reproduce it, but it works on Windows 10 22H2, Windows App SDK 1.6.240829007 :

Image

Iomegan commented 1 day ago

That's so strange. I've others test it as well. For some it works, for others it does not. Is there some kind of Windows settings that could block this?

kmgallahan commented 22 hours ago

GetStorageItemsAsync is broken and the documentation has not been updated. Solution and additional details:

https://github.com/microsoft/microsoft-ui-xaml/issues/9296#issuecomment-1916853813