C++/WinRT WinUI3外部文件拖放异常:拖拽显示禁止符号
WinUI3 管理员权限下拖放外部文件显示禁止符号问题
我曾遇到过相同的问题,但原提问者未给出解决方案。我确认采用了相同的WinUI3拖放实现方法,且应用与Windows资源管理器均以管理员身份运行,但拖拽文件时仍显示禁止符号,无法完成拖放操作。
我的实现代码
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"); } }
当前问题细节
- 已监听几乎所有拖放事件,问题并非数据获取失败,而是拖拽过程中始终显示禁止符号,完全无法完成拖放操作
- 尝试通过原生Windows API解决:设置
WS_EX_ACCEPTFILES扩展样式并捕获WM_DROPFILES消息,此时拖放功能可正常工作,但不确定WinUI3原生实现是否存在消息拦截或拖放限制 - 已排除管理员权限差异问题:应用与资源管理器均以管理员身份运行,且部分微软自带应用在管理员权限下仍可正常拖放
求助内容
希望了解该问题的根本原因,以及基于WinUI3原生API的更优解决方案,避免依赖原生Windows消息钩子。
内容的提问来源于stack exchange,提问作者masterQian
相关产品推荐
相关产品推荐

