c# - Iphone,从 MonoTouch 获取国家列表

标签 c# iphone ios xamarin.ios nslocale

是否可以复制here中的代码?在 MonoTouch 中呢?

到目前为止,这是我尝试过的:

foreach(string countryCode in NSLocale.ISOCountryCodes){
 // How to convert this objective-c code to c#?
 // [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:countryCode]
}

最佳答案

遗憾的是,快速浏览显示 displayNameForKey:value: 是(目前从 4.2.x 开始)从 MonoTouch(和 MonoMac)绑定(bind)中丢失。我会考虑实现它,并会在完成后更新此条目。

更新:解决缺失绑定(bind)的源代码

    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 带来的乐趣!

附注我将更新绑定(bind),以便它将以更适当的 API 包含在 MonoTouch 的 future 版本中;-)

关于c# - Iphone,从 MonoTouch 获取国家列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7577535/

相关文章:

iphone - 我可以摆脱 iPhone 键盘的最后一行或更改某些键的功能吗?

iphone - 从 NSOperation 中取消 NSOperationQueue

c# - 同时拥有 SonarQube 的 C++ 和 c# 插件会导致 SQL Server 出错

c# - Clipboard.SetText() 使用三元运算符

c# - 私有(private)静态和私有(private)方法的困境

ios - UIView 被带到前面后不显示

iphone - bounds 属性给出了错误的信息

ios - 使用ARKit作为“奖励”的普通应用程序?

ios - Apple 拒绝了应用程序 : 5. 2.2 法律、知识产权 - 第三方站点/服务

c# - Graphics.DrawString() 和比例字体的前导填充空间