1

根据MonoMac 文档,签名为NSWorkspace.RecycleUrls

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

根据Apple的NSWorkspace签名文档recycleURLs:completionHandler是:

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

我有一组要回收的 URL。

在这里我很难过 - Apple 文档说第一个参数是 anNSArray但 MonoMac 想要一个NSDictionary. 构造所需NSDictionary值的所有功能(不出所料),而我只有values

除了采用 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();
}
4

1 回答 1

2

Mono 的绑定不正确,我更改了两者并NSWorkspace.RecycleUrls()取而代之NSWorkspace.DuplicateUrls()NSArraymonomac提交 b1eda57NSDictionary

这是生成的代码:

    [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));
            });
        });
    }
于 2012-11-21T01:09:39.080 回答