Iphone, Obtaining a List of countries from MonoTouch

守給你的承諾、 提交于 2019-12-11 13:22:10

问题


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]
}

回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/7577535/iphone-obtaining-a-list-of-countries-from-monotouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!