无法访问受保护成员“Foundation.NSDictionary.NSDictionary(System.IntPtr)" [英] Cannot access protected member 'Foundation.NSDictionary.NSDictionary(System.IntPtr)'

查看:25
本文介绍了无法访问受保护成员“Foundation.NSDictionary.NSDictionary(System.IntPtr)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将我们的 Xamarin 构建的应用程序部署到 Apple 应用商店,因此我们需要将我们的应用程序迁移到 Unified Api 以获得 64 位支持.升级到最新版本的 Xamarin.iOS(版本:8.8.2.4)并将应用程序迁移到 Unified Api 后,我们现在得到以下编译错误:>

无法通过类型为Foundation.NSDictionary"的限定符访问受保护的成员Foundation.NSDictionary.NSDictionary(System.IntPtr)".

错误出现在这一行:

NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));

这个方法的调用:

public static void RegisterDefaultsFromSettingsBundle(){string settingsBundle = NSBundle.MainBundle.PathForResource("Settings", @"bundle");if(settingsBundle == null) {System.Console.WriteLine(@"找不到 Settings.bundle");返回;}NSString keyString = new NSString(@"Key");NSString defaultString = new NSString(@"DefaultValue");NSDictionary settings = NSDictionary.FromFile(Path.Combine(settingsBundle,@"Root.plist"));NSArray 首选项 = (NSArray) settings.ValueForKey(new NSString(@"PreferenceSpecifiers"));NSMutableDictionary defaultsToRegister = new NSMutableDictionary();for (uint i=0; i

谁能解释为什么会发生这种情况以及我如何解决这个问题?

解决方案

根据 Apple 文档PreferenceSpecifiers 是一个字典数组,所以不要这样做:

NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));

你应该能够做到这一点:

NSDictionary prefSpecification = (NSDictionary)preferences.ValueAt(i);

过时

如果您使用新的统一 API,您将必须对所需的类(在本例中为 NSDictionary)进行子类化,才能访问 此 Xamarin 文档.

希望能帮到你

We are trying to deploy our Xamarin built application to the Apple appstore and so we needed to migrate our application to the Unified Api to get 64-bit support. After upgrading to the latest version of Xamarin.iOS (Version: 8.8.2.4) and migrating the app to the Unified Api, we now get the following compile error:

Cannot access protected member 'Foundation.NSDictionary.NSDictionary(System.IntPtr)' via a qualifier of type 'Foundation.NSDictionary'.

The error occurs on this line:

NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));

of this method call:

public static void RegisterDefaultsFromSettingsBundle() 
    {
        string settingsBundle = NSBundle.MainBundle.PathForResource("Settings", @"bundle");
        if(settingsBundle == null) {
            System.Console.WriteLine(@"Could not find Settings.bundle");
            return;
        }
        NSString keyString = new NSString(@"Key");
        NSString defaultString = new NSString(@"DefaultValue");
        NSDictionary settings = NSDictionary.FromFile(Path.Combine(settingsBundle,@"Root.plist"));
        NSArray preferences = (NSArray) settings.ValueForKey(new NSString(@"PreferenceSpecifiers"));
        NSMutableDictionary defaultsToRegister = new NSMutableDictionary();
        for (uint i=0; i<preferences.Count; i++) {
            NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));
            NSString key = (NSString) prefSpecification.ValueForKey(keyString);
            if(key != null) {
                NSObject def = prefSpecification.ValueForKey(defaultString);
                if (def != null) {
                    defaultsToRegister.SetValueForKey(def, key);
                }
             }
        }
        NSUserDefaults.StandardUserDefaults.RegisterDefaults(defaultsToRegister);
    }

Can anyone explain why this is happening and how I can work around this issue?

解决方案

According to Apple docs, PreferenceSpecifiers is an Array of dictionaries, so instead of doing this:

NSDictionary prefSpecification = new NSDictionary(preferences.ValueAt(i));

You should be able to do this:

NSDictionary prefSpecification = (NSDictionary)preferences.ValueAt(i);

Udated

If you are using the new unified API, you will have to subclass the desired class (NSDictionary in this case) to have access to the NSDictionary(IntPtr) constructor as described on this Xamarin docs.

Hope it helps

这篇关于无法访问受保护成员“Foundation.NSDictionary.NSDictionary(System.IntPtr)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆