如何从 Xamarin 中的 iOS DropInteraction 读取 pdfdoc

How to read a pdfdoc from a iOS DropInteraction in Xamarin

喜欢这个 post 我想通过应用程序间的拖放交互接收 pdf 文档。 IUIDropSession 包含 com.adobe.pdf 的标识符,当我将我的 pdf 文件拖到我的组件上时,也会调用 PerformDrop,但不会调用 LoadPdfs。应用程序崩溃。为什么?

[Export("dropInteraction:canHandleSession:")]
public bool CanHandleSession(UIDropInteraction interaction, IUIDropSession session)
{
  return session.CanLoadObjects(typeof(PDFDocument)); // This returns true
}

[Export("dropInteraction:sessionDidUpdate:")]
public UIDropProposal SessionDidUpdate(UIDropInteraction interaction, IUIDropSession session)
{
  return new UIDropProposal(UIDropOperation.Copy);
}

[Export("dropInteraction:performDrop:")]
public void PerformDrop(UIDropInteraction interaction, IUIDropSession session)
{
  session.LoadObjects<PDFDocument>(LoadPdfs); // CRASH WHEN EXECUTING THIS LINE
}

private void LoadPdfs(PDFDocument[] items)
{
  // This is not called
}

这里是 PdfDocument class

class PDFDocument : NSObject, INSItemProviderReading
{
  string identifier;
  NSData data;

  public PDFDocument(NSData pdfData, string typeIdentifier)
  {
     data = pdfData;
     identifier = typeIdentifier;
  }

  [Export("objectWithItemProviderData:typeIdentifier:error:")]
  static public INSItemProviderReading GetObject(NSData data, string indentifier, NSError error)
  {
     return new PDFDocument(data, indentifier);
  }

  [Export("readableTypeIdentifiersForItemProvider")]
  static public string[] ReadableTypeIdentifiers  => new string[]
  {
     "com.adobe.pdf"
  };

  [Export("withItemProviderData")]
  public static object WithItemProviderData(NSData data, string typeIdentifier)
    => new PDFDocument(data, typeIdentifier);
}

崩溃:

=================================================================
    Native Crash Reporting
=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
    0x105c485ac - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/Frameworks/Mono.framework/Mono : mono_dump_native_crash_info
    0x105c3e724 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/Frameworks/Mono.framework/Mono : mono_handle_native_crash
    0x105c4ca2c - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/Frameworks/Mono.framework/Mono : mono_sigsegv_signal_handler_debug
    0x1e0b1fd50 - /usr/lib/system/libsystem_platform.dylib : <redacted>
    0x104e29a34 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : xamarin_get_block_descriptor
    0x104e2976c - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : xamarin_get_block_descriptor
    0x104e296b4 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : xamarin_get_block_descriptor
    0x100d41aac - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : 
    0x100d418c8 - /private/var/containers/Bundle/Application/6363E6AA-015A-4276-8203-4C138D1F9A22/TestFilePicker.iOS.app/TestFilePicker.iOS : 
    0x1898b7db4 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>
    0x189927030 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>
    0x187d4e2ec - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187d4f2f0 - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187cf554c - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187cf5ff0 - /usr/lib/system/libdispatch.dylib : <redacted>
    0x187cffae4 - /usr/lib/system/libdispatch.dylib : <redacted>
    0x1e0b2af38 - /usr/lib/system/libsystem_pthread.dylib : _pthread_wqthread
    0x1e0b2aaa4 - /usr/lib/system/libsystem_pthread.dylib : start_wqthread

=================================================================

尝试设置原始方法签名而不是扩展方法。

session.LoadObjects(new Class(typeof(PDFDocument)), (INSItemProviderReading[] items)=>
{
     PDFDocument[] docs = items as PDFDocument[];
});

参考

https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination?language=objc .


更新

尝试简单地使用以下代码,看看它是否有效。

[Export("dropInteraction:performDrop:")]
public void PerformDrop(UIDropInteraction interaction, IUIDropSession session)
{
    var label = interaction == oddDropInteraction ? OddNumbersLabel : EvenNumbersLabel;
    session.LoadObjects(new Class(typeof(NSString)),strings =>
    {
        ///
    });
}

参考

https://devblogs.microsoft.com/xamarin/drag-and-drop-apis-for-xamarin-apps/