访问Xamarin.iOS Settings.Bundle?

我一直在试图find解决问题的办法,而这个问题应该是非常简单的。 我想要的是在应用程序不运行时可以在iPhone设置菜单中编辑的variables。 基本上是一个包装在iOS GUI中的configuration文件。

这应该是iOS中的一个内置function,虽然我可以find一些相关的方法,但我找不到一个实际的解决scheme。

最接近我得到我想要的是它的工作原理像任何其他variables:应用程序启动时为空,并在应用程序closures时再次被划伤。 在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

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

为此,您需要在应用程序包的顶层目录中创build一个名为“Settings.bundle”的文件夹。 然后,创build一个名为“Root.plist”的新文件。 该文件必须是属性列表的types。 你可以通过右击Settings.bundle,然后添加 – >新build文件… – > iOS(在左边的窗格) – >属性列表。 如果您添加一个空文件,然后将其重命名为.plist,则不会在iPhone的“设置”中显示。

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

你可以在这里find更多关于如何构build设置视图的信息: https : //developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html

请注意图4-2(不需要string文件名)。 另外,在Xamarin编辑器中编辑Root.plist文件,这是更容易的。

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

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

尝试这个:

 NSBundle.MainBundle.PathForResource ("Settings", @"bundle");