[英]Accessing Xamarin.iOS 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
首先,您需要在iPhone设置窗口中显示您的视图。
为此,您需要在应用程序包的顶级目录中创建名为“Settings.bundle”的文件夹。 然后,创建名为“Root.plist”的新文件。 该文件必须是属性列表类型。 您可以通过右键单击Settings.bundle,然后单击添加 - >新建文件... - > iOS(在左窗格中) - >属性列表来执行此操作。 如果您添加一个空文件,然后将其重命名为.plist,它将不会显示在iPhone的设置中。
Root.plist中的第一个元素必须是一个Array,它必须包含Dictionaries。
您可以在此处找到有关如何构建设置视图的更多信息: https : //developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html
注意图4-2(不需要字符串文件名)。 另外,在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()
(这是唯一的公共方法),然后从类中的属性中获取值。
试试这个:
NSBundle.MainBundle.PathForResource ("Settings", @"bundle");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.