c# - Xcode Cocoa - 从 NSTableView 拖放到 Finder

标签 c# objective-c xcode cocoa xamarin

我希望我的 MacOS 应用程序能够将项目从 NSTableView 拖动到其他应用程序,例如 Logic Pro X、Finder 等。 此 TableView 中的项目是我创建的类,它们代表我的硬盘上的文件。

public class AudioFile
{
    #region Computed Propoperties
    public string Filename { get; set; } = "";
    public string Filepath { get; set; } = "";
    #endregion

    public AudioFile()
    {
    }

    public AudioFile(string filename, string filepath)
    {
        this.Filename = filename;
        this.Filepath = filepath;
    }
}

不幸的是,我找不到 Swift 或 Objective-C 的解决方案,可以将其转换为 C# (Xamarin)。有谁知道或有一些代码可以在这里提供帮助吗?

感谢您的帮助!

最佳答案

我对 C# 一无所知,但您要求使用 Swift 或 Objective-C 提供解决方案。我可以帮忙!下面是 Swift 4。

首先,确保您的ViewController是表格 View 的数据源:

class ViewController: NSViewController, NSTableViewDataSource

您还需要在代码或 IB 中建立该连接。

然后,您需要将表格 View 设置为拖动源。选择您想要的操作,通常是 .move.copy:

tableView.setDraggingSourceOperationMask(.move, forLocal: false)

此示例假设您使用 ArrayController 来管理 tableView 的内容。你确实应该这样做,这会让很多事情变得更容易。另外,此示例用于拖动多个文件。 (它适用于单个文件,但如果您只想拖动一个文件,还有其他方法。)

在您的 ViewController 类中,实现此方法:

func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
    var filePaths = [String]()

    // Swift 4 hack--the FilenamesPboardType is missing
    let NSFilenamesPboardTypeTemp = NSPasteboard.PasteboardType("NSFilenamesPboardType")
    pboard.addTypes([NSFilenamesPboardTypeTemp], owner: nil)

    if let audioFiles = audioFilesArrayController.arrangedObjects as? [AudioFile] {
        for i in rowIndexes {
            filePaths.append(audioFiles[i].Filepath)
        }
    }

    pboard.setPropertyList(filePaths, forType: NSFilenamesPboardTypeTemp)

    return true
}

您可以了解有关 NSFilenamesPboardTypeTemp hack here 的更多信息.

就是这样!重新编译,您应该能够通过将一个或多个文件拖到 Finder 窗口来移动它们。简单的。 :-)

关于c# - Xcode Cocoa - 从 NSTableView 拖放到 Finder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734914/

相关文章:

ios - 如何使用 replaceregexp 从 ANT 脚本更新 Xcode 项目 info.plist 构建版本

c# - 使用具有变量名称的项目反序列化数组

objective-c - 在 Objective-C 中创建私有(private)属性

Xcode 团队未注册 Apple Developer Program

ios - 为什么 UIPageViewController 中 UITableView 的内容插入在交互后立即变得困惑?

objective-c - NSMutableArray 到 NSString 并将 NSString 传递到另一个 View IOS5.1

objective-c - OSX 上自动更新应用程序的常见更新程序?

c# - 将 System.Net.Http.Formatting 从 Version=4.0.0.0 更新到 Version=5.2.3.0

c# - 如何在 TFS 自定义构建事件中获取构建项目

c# - IQueryable for entities .Where(属性在本地数组中)