是否可以在 MonoTouch 中复制此处的代码?
这是我迄今为止尝试过的:
foreach(string countryCode in NSLocale.ISOCountryCodes){
// How to convert this objective-c code to c#?
// [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:countryCode]
}
可悲的是,快速浏览显示MonoTouch(和 MonoMac)绑定中缺少displayNameForKey:value:(当前为 4.2.x)。我将研究实现它,并在完成后更新此条目。
更新:解决丢失绑定的源代码
public void DisplayCountryCodeNames ()
{
NSLocale current = NSLocale.CurrentLocale;
IntPtr handle = current.Handle;
IntPtr selDisplayNameForKeyValue = new Selector ("displayNameForKey:value:").Handle;
foreach (var countryCode in NSLocale.ISOCountryCodes) {
using (var key = new NSString ("kCFLocaleCountryCodeKey")) {
using (var nsvalue = new NSString (countryCode)) {
string ret = NSString.FromHandle (MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr (handle, selDisplayNameForKeyValue, key.Handle, nsvalue.Handle));
Console.WriteLine ("{0} -> {1}", countryCode, ret);
}
}
}
}
适应您的喜好,享受 MonoTouch 的乐趣!
ps 我将更新绑定,以便它将以更合适的API 包含在 MonoTouch 的未来版本中;-)
是的,这当然是可能的。MonoTouch 将 iOS API 封装在 C# 类中,以便您可以在 C# 中执行您在 Objective-C 中可以执行的任何操作。不幸的是,您必须下载试用版才能获得文档。我想代码看起来像这样:
foreach(string countryCode in NSLocale.ISOCountryCodes){
string CountryName = NSLocale.displayNameForKey(NSLocaleCountryCode, countryCode);
}