根据the MonoMac documentationNSWorkspace.RecycleUrls的签名为:

[MonoMac.Foundation.Export("recycleURLs:completionHandler:")]
public virtual void RecycleUrls (MonoMac.Foundation.NSDictionary urls, NSWorkspaceUrlHandler completionHandler)


根据Apple's documentation for NSWorkspacerecycleURLs:completionHandler的签名为:

- (void)recycleURLs:(NSArray *)URLs completionHandler:(void (^)(NSDictionary *newURLs, NSError *error))handler


我有一个要回收的URL数组。

在这里,我很困惑-Apple文档说第一个参数是NSArray,但是MonoMac需要一个NSDictionary。构造NSDictionary的所有函数都需要键和值(毫不奇怪),而我只有值。

除了采用MonoMac的RecycleUrls实现(如下所示)并将其重写为使用NSArray之外,还需要做其他事情吗?

[Export("recycleURLs:completionHandler:")]
public unsafe virtual void RecycleUrls(NSDictionary urls, NSWorkspaceUrlHandler completionHandler)
{
    if (urls == null)
    {
        throw new ArgumentNullException("urls");
    }
    if (completionHandler == null)
    {
        throw new ArgumentNullException("completionHandler");
    }
    BlockLiteral blockLiteral = default(BlockLiteral);
    blockLiteral.SetupBlock(NSWorkspace.static_InnerNSWorkspaceUrlHandler, completionHandler);
    if (this.IsDirectBinding)
    {
        Messaging.void_objc_msgSend_IntPtr_IntPtr(base.Handle, NSWorkspace.selRecycleURLsCompletionHandler_, urls.Handle, (IntPtr)((void*)(&blockLiteral)));
    }
    else
    {
        Messaging.void_objc_msgSendSuper_IntPtr_IntPtr(base.SuperHandle, NSWorkspace.selRecycleURLsCompletionHandler_, urls.Handle, (IntPtr)((void*)(&blockLiteral)));
    }
    blockLiteral.CleanupBlock();
}

最佳答案

Mono的绑定不正确,我同时更改了NSWorkspace.RecycleUrls()NSWorkspace.DuplicateUrls()以采用NSArray而不是NSDictionarymonomac commit b1eda57

这是生成的代码:

    [Export ("recycleURLs:completionHandler:")]
    public unsafe virtual void RecycleUrls (NSArray urls, NSWorkspaceUrlHandler completionHandler)
    {
        if (urls == null)
            throw new ArgumentNullException ("urls");
        if (completionHandler == null)
            throw new ArgumentNullException ("completionHandler");
        BlockLiteral *block_ptr_completionHandler;
        BlockLiteral block_completionHandler;
        block_completionHandler = new BlockLiteral ();
        block_ptr_completionHandler = &block_completionHandler;
        block_completionHandler.SetupBlock (static_InnerNSWorkspaceUrlHandler, completionHandler);

        if (IsDirectBinding) {
            MonoMac.ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_IntPtr (this.Handle, selRecycleURLsCompletionHandler_, urls.Handle, (IntPtr) block_ptr_completionHandler);
        } else {
            MonoMac.ObjCRuntime.Messaging.void_objc_msgSendSuper_IntPtr_IntPtr (this.SuperHandle, selRecycleURLsCompletionHandler_, urls.Handle, (IntPtr) block_ptr_completionHandler);
        }
        block_ptr_completionHandler->CleanupBlock ();

    }

    [Export ("duplicateURLs:completionHandler:")]
    public unsafe virtual void DuplicateUrls (NSArray urls, NSWorkspaceUrlHandler completionHandler)
    {
        if (urls == null)
            throw new ArgumentNullException ("urls");
        if (completionHandler == null)
            throw new ArgumentNullException ("completionHandler");
        BlockLiteral *block_ptr_completionHandler;
        BlockLiteral block_completionHandler;
        block_completionHandler = new BlockLiteral ();
        block_ptr_completionHandler = &block_completionHandler;
        block_completionHandler.SetupBlock (static_InnerNSWorkspaceUrlHandler, completionHandler);

        if (IsDirectBinding) {
            MonoMac.ObjCRuntime.Messaging.void_objc_msgSend_IntPtr_IntPtr (this.Handle, selDuplicateURLsCompletionHandler_, urls.Handle, (IntPtr) block_ptr_completionHandler);
        } else {
            MonoMac.ObjCRuntime.Messaging.void_objc_msgSendSuper_IntPtr_IntPtr (this.SuperHandle, selDuplicateURLsCompletionHandler_, urls.Handle, (IntPtr) block_ptr_completionHandler);
        }
        block_ptr_completionHandler->CleanupBlock ();

    }


快速测试:

    void TestUrls ()
    {
        var path = Path.GetTempFileName ();
        Console.WriteLine ("TEST: {0} {1}", path, File.Exists (path));

        var array = new NSMutableArray ();
        var url = new NSUrl ("file://" + path);
        array.Add (url);

        var workspace = NSWorkspace.SharedWorkspace;
        workspace.DuplicateUrls (array, (urls, error) => {
            Console.WriteLine ("DUPLICATED: {0} {1}", urls, error);
            array.Add (urls.Values [0]);

            workspace.RecycleUrls (array, (urls2, error2) => {
                Console.WriteLine ("RECYCLED: {0} {1} {2}",
                                   urls2, error2, File.Exists (path));
            });
        });
    }

关于c# - MonoMac NSWorkspace.RecycleUrls与recycleURLs:completionHandler,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13453957/

10-12 14:52