访问Xamarin.iOS Settings.Bundle? [英] Accessing Xamarin.iOS Settings.Bundle?

查看:176
本文介绍了访问Xamarin.iOS Settings.Bundle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying for quite a while to find a solution to a problem which ought to be pretty straight forward. What I want is variables that can be edited in the iPhone settings menu while the app is not running. Basically a config file wrapped in the iOS GUI.

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

This is supposed to be an built-in feature in iOS, and while I can find some methods related to it, I can't find an actual solution.

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

The closest I've been to getting what I want is where it works like any other variable: Empty on application start, and gets scratched again on application close. And still not visible in the iPhone Settings window.

这是我的代码:

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文件:

And my Settings.Bundle Root.pList file:

  <?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并且知道这是如何工作的人?

Anyone out there who's been messing with Xamarin iOS and knows how this works?

推荐答案

编辑:以下是Xamarin的一个有效项目: https://github.com/xamarin/monotouch-samples/tree/master/AppPrefs

Here is a working project in Xamarin: https://github.com/xamarin/monotouch-samples/tree/master/AppPrefs

首先,您需要在iPhone设置窗口中显示您的视图。

First, you need to make your view visible in the iPhone Settings window.

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

To do that, you need to create a folder named "Settings.bundle" in the top-level directory of your app’s bundle. Then, create new file named "Root.plist". The file has to be of the type Property List. You do that by right-clicking Settings.bundle, then Add -> New File... -> iOS (on the left pane) -> Property List. If you add an empty file and then rename it as .plist, it won't show in iPhone's Settings.

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

The first element in your Root.plist has to be an Array, and it must contain Dictionaries.

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

You can find more info here on how to construct your Settings View: https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html

注意图4-2(不需要字符串文件名)。另外,在Xamarin编辑器中编辑Root.plist文件会更容易。

Pay attention to Figure 4-2 (Strings Filename is not necessary). Also, edit the Root.plist file in the Xamarin editor, it is much easier.

要从新创建的设置中获取值,您可以使用此类(添加为你想要的许多属性):

To get values from the newly created Settings, you can use this class (Add as many properties as you want):

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( )(这是唯一的公共方法),然后从类中的属性中获取值。

You just call SetUpByPreferences() (which is the only public method), and then get the values from the properties in the class.

这篇关于访问Xamarin.iOS Settings.Bundle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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