就像在這篇文章中一樣,我想通過應用間拖放互動接收 pdf 檔案。當我將我的 pdf 檔案拖到我的組件上時,IUIDropSession 包含com.adobe.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 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
=================================================================
uj5u.com熱心網友回復:
嘗試設定原始方法簽名而不是擴展方法。
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/
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/336754.html
