c# - 访问 Xamarin.iOS Settings.Bundle?

标签 c# ios iphone xamarin settings.bundle

我已经尝试了很长一段时间来找到一个应该非常简单的问题的解决方案。 我想要的是可以在应用程序未运行时在 iPhone 设置菜单中编辑的变量。基本上是一个包装在 iOS GUI 中的配置文件。

这应该是 iOS 中的一个内置功能,虽然我可以找到一些与之相关的方法,但我找不到实际的解决方案。

我最接近我想要的是它像任何其他变量一样工作的地方:应用程序启动时为空,应用程序关闭时再次被划伤。并且在 iPhone 设置窗口中仍然不可见。

这是我的代码:

private void LoadSettingsFromIOS()
{
    // This is where it works like any other variable. Aka. gets scratched on app closing.
    _thisUser.SetValueForKey(new NSString("Blargh"), new NSString("SaveCredentials"));
    string stringForKey = _thisUser.StringForKey("SaveCredentials");

    // This is where I'm supposed to be able to load the data from settings and set the checkbox's 'On' state to the value. Currently it always returns False.
    bool saveCredentials = _thisUser.BoolForKey("SaveCredentials");
    chckBoxRememberMe.On = saveCredentials;
}

还有我的 Settings.Bundle Root.pList 文件:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
      <key>PreferenceSpecifiers</key>
      <array>
          <dict>
              <key>Type</key>
              <string>PSToggleSwitchSpecifier</string>
              <key>Title</key>
              <string>Credentials</string>
              <key>Key</key>
              <string>SaveCredentials</string>
              <key>DefaultValue</key>
              <true/>
          </dict>
      </array>
      <key>StringsTable</key>
      <string>Root</string>
  </dict>
  </plist>

有人一直在搞乱 Xamarin iOS 并且知道它是如何工作的吗?

最佳答案

编辑:这是 Xamarin 中的一个工作项目:https://github.com/xamarin/monotouch-samples/tree/master/AppPrefs

首先,您需要让您的 View 在 iPhone 设置窗口中可见。

为此,您需要在应用程序包的顶级目录中创建一个名为“Settings.bundle”的文件夹。然后,创建名为“Root.plist”的新文件。文件必须是属性列表类型。 您可以通过右键单击 Settings.bundle,然后添加 -> 新文件... -> iOS(在左 Pane 中)-> 属性列表来完成此操作。如果您添加一个空文件,然后将其重命名为 .plist,它不会显示在 iPhone 的设置中。

Root.plist 中的第一个元素必须是数组,并且必须包含字典。

您可以在此处找到有关如何build设置 View 的更多信息: https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html

注意图4-2(不需要Strings Filename)。另外,在 Xamarin 编辑器中编辑 Root.plist 文件,就容易多了。

要从新创建的设置中获取值,您可以使用此类(添加任意数量的属性):

public class Settings
{
    public static string ApiPath { get; private set; }

    const string API_PATH_KEY = "serverAddress"; //this needs to be the Identifier of the field in the Root.plist

    public static void SetUpByPreferences()
    {
        var testVal = NSUserDefaults.StandardUserDefaults.StringForKey(API_PATH_KEY);

        if (testVal == null)
            LoadDefaultValues();
        else
            LoadEditedValues();

        SavePreferences();
    }

    static void LoadDefaultValues()
    {
        var settingsDict = new NSDictionary(NSBundle.MainBundle.PathForResource("Settings.bundle/Root.plist", null));

        if (settingsDict != null)
        {
            var prefSpecifierArray = settingsDict[(NSString)"PreferenceSpecifiers"] as NSArray;

            if (prefSpecifierArray != null)
            {
                foreach (var prefItem in NSArray.FromArray<NSDictionary>(prefSpecifierArray))
                {
                    var key = prefItem[(NSString)"Key"] as NSString;

                    if (key == null)
                        continue;

                    var value = prefItem[(NSString)"DefaultValue"];

                    if (value == null)
                        continue;

                    switch (key.ToString())
                    {
                        case API_PATH_KEY:
                            ApiPath = value.ToString();
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    static void LoadEditedValues()
    {
        ApiPath = NSUserDefaults.StandardUserDefaults.StringForKey(API_PATH_KEY);
    }

    //Save new preferences to Settings
    static void SavePreferences()
    {
        var appDefaults = NSDictionary.FromObjectsAndKeys(new object[] {
            new NSString(ApiPath)
        }, new object[] {
            API_PATH_KEY
        });

        NSUserDefaults.StandardUserDefaults.RegisterDefaults(appDefaults);
        NSUserDefaults.StandardUserDefaults.Synchronize();
    }
}

您只需调用 SetUpByPreferences()(这是唯一的公共(public)方法),然后从类中的属性中获取值。

关于c# - 访问 Xamarin.iOS Settings.Bundle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998894/

相关文章:

c# - 如何使用 iTextSharp 将表格插入 PDF 文档?

c# - 在 C# 中使用相同 UDP 端口的多个程序/实例

c# - 使用自定义类绑定(bind)创建复选框组合框

iphone - 具有一对多关系的核心数据自引用实体

c# - 如何将媒体属性添加到 ASP.NET WebResource.axd Http 处理程序的 CSS LINK html 标记

ios - 停止向 Apple 发送推送通知以进行传递

C# 使用寄存器

iphone - Sybase ULtralite DB 和 iOS 核心数据

iOS:使对象数组在屏幕上移动

ios - 从 UIAlertAction 处理程序更新 UILabel 在 Swift 4 中不起作用