macos - Xamarin 中的 MDItem 类

标签 macos cocoa xamarin xamarin.mac

我正在尝试从文件中读取评论字段。 enter image description here

找到this thread关于如何做到这一点,但在Xamarin中找不到MDItem相应的类。

有什么想法可以在哪里找到它或者用什么来代替吗?

最佳答案

某些 CoreServices 未包含在当前 Xamarin.Mac 中 (版本:2.10.0.57)。

它们中的大多数传递基于CFxxxx的引用,因此它们很容易通过一些互操作调用来包装和实现。

基于Finder的文件注释

enter image description here

代码示例:

var fileURL = NSUrl.FromString("/Users/sushi/Desktop/DFeedback_FeedBack.png");
var mMDItemRef = MDItemCreateWithURL(IntPtr.Zero, fileURL.Handle);

var mCFTypeRef = MDItemCopyAttribute(mMDItemRef, new CFString("kMDItemFinderComment").Handle);
var finderComment = NSString.FromHandle(mCFTypeRef);
Console.WriteLine(finderComment);

注意:应在生产代码中测试互操作调用是否为空,请参阅头文件中的返回签名注释。

应用程序输出:

StackOverflow

互操作设置:

// @function MDItemCreateWithURL
// Returns an metadata item for the given path.
// @param allocator The CFAllocator which should be used to allocate
// memory for the query and its sub-storage.This
// parameter may be NULL in which case the current default
// CFAllocator is used.
// @param url A url to the file for which to create the MDItem.
// [[Currently, the file must exist.MDItemRefs may or
// may not be uniqued.Use CFEqual() to compare them.]]
// @result An MDItemRef, or NULL on failure.

//MD_EXPORT MDItemRef MDItemCreateWithURL(CFAllocatorRef allocator, CFURLRef url) AVAILABLE_MAC_OS_X_VERSION_10_6_AND_LATER;
[DllImport(Constants.CoreServicesLibrary)]
extern static /* MDItemRef */ IntPtr MDItemCreateWithURL(/* CFAllocatorRef __nullable */ IntPtr allocator, /* CFURLRef */ IntPtr inURL);

//@function MDItemCopyAttribute
//Returns the value of the given attribute for the item.
//@param item The item to be interrogated.
//@param name The name of the desired attribute.
//@result A CFTypeRef, or NULL on failure, or if the attribute
//does not exist, of if the attribute is not readable.

//MD_EXPORT CFTypeRef MDItemCopyAttribute(MDItemRef item, CFStringRef name) MD_AVAIL;
[DllImport(Constants.CoreServicesLibrary)]
extern static /* CFTypeRef */ IntPtr MDItemCopyAttribute(/* MDItemRef */ IntPtr item, /* CFStringRef */ IntPtr name);

Xcode Obj-C header 引用:

MDItem.h

本地 Xcode 引用::/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Headers

关于macos - Xamarin 中的 MDItem 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952536/

相关文章:

objective-c - 如何以编程方式向 OS X 应用程序添加红绿灯按钮

c# - 在 iOS 上录制实时音频流

ios - 命令/bin/sh 失败,退出代码为 133 - CocoaPods 错误 macOS Sierra

objective-c - 以编程方式生成适合打印机的输出

ios - macOS swift AVCaptureSession sessionPreset 不起作用

objective-c - 如何将小图像嵌入到我的应用程序中?

swift - 在 Swift 中从 Cocoa App 运行终端命令失败 : "command not found" but works using Command Line Tool in swift

ios - Xcode - 如何修复 'NSUnknownKeyException' ,原因 : … this class is not key value coding-compliant for the key X"error?

android - 指定的任务可执行位置...无效

c# - 如何声明公共(public)静态字节?