You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI MacCatalyst平台文件拖放(DragOver/Drop)事件失效问题

解决.NET MAUI MacCatalyst平台Frame控件Drop手势不触发问题

针对MacCatalyst平台下Frame控件的DropGestureRecognizer事件未触发的问题,可通过以下步骤解决:

1. 配置文件拖拽权限

在MacCatalyst项目的Info.plist中添加文件类型支持,确保应用能接收文件拖拽:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>Supported Files</string>
        <key>LSHandlerRank</key>
        <string>Alternate</string>
        <key>LSItemContentTypes</key>
        <array>
            <!-- 支持所有文件类型,可替换为具体类型如public.image、public.pdf等 -->
            <string>public.item</string>
        </array>
    </dict>
</array>

2. 自定义Frame的MacCatalyst Handler

创建自定义Handler,桥接原生NSView的拖拽事件到MAUI的DropGestureRecognizer:

using Microsoft.Maui.Platform;
using UIKit;
using Foundation;
using System.Linq;

namespace YourAppNamespace.Platforms.MacCatalyst
{
    public class DropEnabledFrameHandler : FrameHandler
    {
        protected override void ConnectHandler(MauiFrame platformView)
        {
            base.ConnectHandler(platformView);
            
            // 注册接受文件URL类型的拖拽
            platformView.RegisterForDraggedTypes(new[] { NSFileType.FileURL });
            platformView.Delegate = new DropFrameDelegate(platformView, VirtualView);
        }

        protected override void DisconnectHandler(MauiFrame platformView)
        {
            platformView.Delegate = null;
            base.DisconnectHandler(platformView);
        }
    }

    public class DropFrameDelegate : UIViewDelegate
    {
        private readonly MauiFrame _platformView;
        private readonly IFrame _virtualView;

        public DropFrameDelegate(MauiFrame platformView, IFrame virtualView)
        {
            _platformView = platformView;
            _virtualView = virtualView;
        }

        public override NSDragOperation DraggingEntered(UIView sender, NSDraggingInfo info)
        {
            // 触发MAUI的DragOver事件
            var dropGesture = _virtualView.GestureRecognizers.OfType<DropGestureRecognizer>().FirstOrDefault();
            dropGesture?.SendDragOver(new DragEventArgs(DataPackageOperation.Copy, new DataPackageView(info.ItemProvider)));
            return NSDragOperation.Copy;
        }

        public override void PerformDrop(UIView sender, NSDraggingInfo info)
        {
            // 触发MAUI的Drop事件
            var dropGesture = _virtualView.GestureRecognizers.OfType<DropGestureRecognizer>().FirstOrDefault();
            dropGesture?.SendDrop(new DropEventArgs(new DataPackageView(info.ItemProvider)));
        }
    }
}

3. 注册自定义Handler

在MauiProgram.cs中注册自定义Handler:

using YourAppNamespace.Platforms.MacCatalyst;

builder.ConfigureMauiHandlers(handlers =>
{
#if MACCATALYST
    handlers.AddHandler<Frame, DropEnabledFrameHandler>();
#endif
});

4. 基础检查

  • 确保Frame及父级FlexLayout的IsEnabled属性为True(默认值为True,若手动修改过需确认)
  • 确认DropGestureRecognizer的AllowDrop已设为True

内容的提问来源于stack exchange,提问作者Gustavo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 03:01:11