Xamarin 中的 MDItem Class

MDItem Class In Xamarin

我正在尝试从文件中读取注释字段。

找到了如何操作,但是在Xamarin中找不到MDItem对应的class。

知道在哪里可以找到它或用什么代替吗?

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

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

Finder-based 文件评论

代码示例:

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);

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

应用程序输出:

Whosebug

互操作设置:

// @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);

XcodeObj-CHeader参考:

MDItem.h

本地 Xcode Ref:: /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