I saw the same problem, but the author did not provide a solution.
I am sure that all methods are the same as that problem, and all processes are using the Administrator.
But it doesn't work, dragging the file still shows X
Reference: How to drag and drop external files into WinUI3 apps?
this is a example.
MainWindow.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Window x:Class="App1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Border Width="300" Height="300"
HorizontalAlignment="Center"
VerticalAlignment="Center" AllowDrop="True"
Background="Pink" CanDrag="True"
DragEnter="Border_DragEnter"
DragLeave="Border_DragLeave"
DragOver="Border_DragOver"
DragStarting="Border_DragStarting"
Drop="Border_Drop">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Drop files here!" />
</Border>
</Window>
MainWindow.xaml.cpp
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
namespace winrt::App1::implementation {
Windows::Foundation::IAsyncAction MainWindow::Border_Drop(IInspectable const&, DragEventArgs const& e) {
if (e.DataView().Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::StorageItems())) {
Windows::Foundation::Collections::IVectorView<Windows::Storage::IStorageItem> items
= co_await e.DataView().GetStorageItemsAsync();
for (auto item : items) {
auto file = item.as<Windows::Storage::StorageFile>();
OutputDebugStringW(file.Path().data());
}
}
}
void MainWindow::Border_DragOver(IInspectable const&, DragEventArgs const& e) {
OutputDebugStringW(L"DragOver\n");
e.AcceptedOperation(Windows::ApplicationModel::DataTransfer::DataPackageOperation::Copy);
}
void MainWindow::Border_DragEnter(IInspectable const&, DragEventArgs const& e) {
OutputDebugStringW(L"DragEnter\n");
}
void MainWindow::Border_DragLeave(IInspectable const&, DragEventArgs const& e) {
OutputDebugStringW(L"DragLeave\n");
}
void MainWindow::Border_DragStarting(UIElement const&, DragStartingEventArgs const&) {
OutputDebugStringW(L"DragStarting\n");
}
}
I have listened to almost all the events, and the current issue is not data acquisition, but displaying prohibition symbols during drag and drop. I am unable to complete the drag and drop at all.
I see another question that is related to Windows preferences, but I haven't found the specific content yet.
I saw others saying it was an issue with running the program as an administrator. But my app and Windows Explorer are both running as administrators, so there should be no permission issues. And for some Microsoft apps, even when running as an administrator, they can still be dragged and dropped, not only my app.
I tried using hook to capture native WM_DROPFILES message, After setting the extension style WS_EX_ACCEPTFILES through SetWindowLong. I can drag and drop it now. I don't know if the implementation of WinUI3 has blocked messages or does not allow drag and drop of files