0
我正在獲取默認文本,而不是嘗試訪問共享首選項時保存的實際文本。我已經測試過,它在保存時返回true,所以我非常確定問題在於從首選項文件中讀取。Android Xamarin:無法從SharedPreferences中讀取
偏好類
public class SaveWarningMessage : Activity
{
private ISharedPreferences myPref;
private ISharedPreferencesEditor myPrefEditor;
private Context myContext;
public void MyAppPref(Context context)
{
this.myContext = context;
myPref = PreferenceManager.GetDefaultSharedPreferences(myContext);
myPrefEditor = myPref.Edit();
}
public void SaveString(string text)
{
myPrefEditor.PutString("warning text", text);
var returnValue = myPrefEditor.Commit();
}
public string GetString()
{
return myPref.GetString("warning text", "could not get pref");
}
}
}
類儲蓄偏好:
string warningText = Intent.GetStringExtra("warningText");
Context mContext = Android.App.Application.Context;
SaveWarningMessage classInstans = new SaveWarningMessage();
classInstans.MyAppPref(mContext);
classInstans.SaveString(warningText);
從偏好的課外閱讀:
Context mContext = Android.App.Application.Context;
SaveWarningMessage classInstans = new SaveWarningMessage();
classInstans.MyAppPref(mContext);
string message = classInstans.GetString();
您的意見,使有很大的意義,我實施了變化,但它仍然無法正常工作,我現在更加困惑......但是thx –
@MajaOkholm所以當你調用'classInstans2.GetString();'你得到'不能獲取pref'而不是'SomeStringToSave'? – SushiHangover
@MajaOkholm說,通常我也使用完全不同的方法,可能你很好地改變了你的代碼,嘗試我在我的答案中寫的東西,我真的很好奇,看看我是否確定了它。 –