Iphone,从 MonoTouch 获取国家列表

发布于 2024-12-06 14:32:13 字数 385 浏览 2 评论 0原文

是否可以复制此处中的代码在 MonoTouch 中吗?

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

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

Is it possible to replicate what the code in here does in MonoTouch?

Here is what I've tried so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里泪两行 2024-12-13 14:32:13

遗憾的是,快速浏览一下就会发现 displayNameForKey:value: 从 MonoTouch(和 MonoMac)绑定中缺失(目前从 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 我将更新绑定,以便将其包含在 MonoTouch 未来版本中的更适当 API 中;-)

Sadly a quick look shows that displayNameForKey:value: to be (currently as of 4.2.x) missing from MonoTouch (and MonoMac) bindings. I'll look into implementing it and will update this entry once done.

UPDATE : Source code to work around the missing binding

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

Adapt to your liking and have fun with MonoTouch!

p.s. I'll update the bindings so it will be included in future releases for MonoTouch in a more proper API ;-)

瑾兮 2024-12-13 14:32:13

是的,这当然是可能的。 MonoTouch 将 iOS API 包装在 C# 类中,以便您可以在 C# 中执行在 Objective-C 中可以执行的任何操作。不幸的是,您必须下载试用版才能获取文档。我想代码会是这样的:

foreach(string countryCode in NSLocale.ISOCountryCodes){
  string CountryName = NSLocale.displayNameForKey(NSLocaleCountryCode, countryCode);
}

Yes, it is certainly possible. MonoTouch wraps the iOS API in C# classes so that you can do anything in C# that you can do in Objective-C. Unfortunately you have to download the trial version to get the documentation. I imagine the code would look something like this:

foreach(string countryCode in NSLocale.ISOCountryCodes){
  string CountryName = NSLocale.displayNameForKey(NSLocaleCountryCode, countryCode);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文