我正在尝试将字符串保存到共享首选项.我尝试这样做的方式如下:
ISharedPreferences _prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor _editor = _prefs.Edit();
_editor.PutString("myString", "123");
_editor.Commit();
Run Code Online (Sandbox Code Playgroud)
再往下我试着打印出我的字符串:
_txtView.Text = _prefs.GetString("myString", "Can't find string");
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我的textview现在正确打印出"123".所以一切都按预期工作.然而; 共享偏好的意义在于它是持久的.因此,如果我现在尝试注释掉以下两行:
_editor.PutString("myString", "123");
_editor.Commit();
Run Code Online (Sandbox Code Playgroud)
然后再次构建,我的textview显示"找不到字符串".所以由于某种原因字符串没有被保存?谁知道为什么会这样?
谢谢!