Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Iomegan opened this issue Oct 30, 2024 · 9 comments
Open
Labels

Comments

@Iomegan
Copy link

Iomegan commented Oct 30, 2024

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

@Iomegan Iomegan added the bug Something isn't working label Oct 30, 2024
Copy link

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.

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Oct 30, 2024
@Iomegan Iomegan changed the title Bug title WinUI 3 Desktop: Drag and drop file from Explorer to app window does not work Oct 30, 2024
@karkarl karkarl added Regression area-DragAndDrop and removed needs-triage Issue needs to be triaged by the area owners labels Oct 31, 2024
@castorix
Copy link

castorix commented Nov 1, 2024

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

Image

@Iomegan
Copy link
Author

Iomegan commented Nov 1, 2024

That's so strange. I've let 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
Copy link
Contributor

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

#9296 (comment)

@Iomegan
Copy link
Author

Iomegan commented Nov 3, 2024

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

#9296 (comment)

Thanks for pointing this out. However neither Grid_Drop nor Grid_DragOver or Grid_DragEnter is being called, so I don't even come to the point where it could crash.

@kmgallahan
Copy link
Contributor

@Iomegan The problem is that all of the pointer events get broken and stop functioning correctly as you are experiencing once GetStorageItemsAsync malfunctions. I would recommend you give the workaround a try real quick just to see if it helps.

@Iomegan
Copy link
Author

Iomegan commented Nov 3, 2024

@kmgallahan I did.

<StackPanel AllowDrop="True"  DragEnter="Grid_DragEnter" Background="LightGray">
    <TextBlock
     x:Name="FileNameTextBlock"
     Text="Drag a file here"
     HorizontalAlignment="Center"
     VerticalAlignment="Center"
     FontSize="20"/>
</StackPanel>
private void Grid_DragEnter(object sender, DragEventArgs e)
{
    Debug.WriteLine("Grid_DragEnter");
}

Grid_DragEnter is not being called and I don't call GetStorageItemsAsync at all. Clean restart. Nothing. I even installed a fresh new copy of Win 11 Pro and nothing but Visual Studio in a VM, same result.

@Iomegan
Copy link
Author

Iomegan commented Nov 4, 2024

Screen recording: Image

@Iomegan
Copy link
Author

Iomegan commented Nov 4, 2024

Changing EnableLUA in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System to 1, made it work after a restart. Why is that? Shouldn't basic features like drag and drop work without enabling LUC? Also all other drag and drop features are workin with LUA disabled. (Explorer, Taskbar, Edge and so on...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants