OGeek|极客世界-中国程序员成长平台

标题: c# - 如何从 UICollectionElementKindSection.Header 转换为 NSString? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:47
标题: c# - 如何从 UICollectionElementKindSection.Header 转换为 NSString?

我正在使用 Xamarin.iOS。如何将 UICollectionElementKindSection.Header 转换为 NSString

Error CS1503: Argument #1' cannot convertUIKit.UICollectionElementKindSection' expression to type `Foundation.NSString' (CS1503)

我尝试了以下方法:

UICollectionElementKindSection.Header.ToString();
(NSString)UICollectionElementKindSection.Header;
(string)UICollectionElementKindSection.Header;

每次我遇到构建错误。



Best Answer-推荐答案


为了类型安全,UICollectionElementKindSectionHeader(和其他值)被映射到枚举,UICollectionElementKindSectionKey。这使得代码完成变得更加容易(在 IDE 中)并消除了代码中潜在的拼写错误。

将枚举值转换为 string(然后是 NSString)是可能的 - 但这不会创建相同的 NSString 常量使用 ObjC 应用程序(甚至更少,因为有时,Apple 使用指针而不是字符串内容来进行常量比较)。

如果您需要使用不使用 enum 但需要该常量的 API,您可以这样做:

IntPtr uikit = Dlfcn.dlopen (Constants.UIKitLibrary, 0);
NSString header = Dlfcn.GetStringConstant (uikit, "UICollectionElementKindSectionHeader");

注意:如果这是 Xamarin.iOS.dll 的 API 部分,请告诉我们 know .我们要么公开常量,要么提供一个接受枚举的重载。

关于c# - 如何从 UICollectionElementKindSection.Header 转换为 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29163803/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4