如何在 Xamarin.Android 中使用 SharedPreferences?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26668509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:02:32  来源:igfitidea点击:

How do I use SharedPreferences in Xamarin.Android?

androidxamarinxamarin.android

提问by Richard Le Mesurier

I want to save and retrieve some application settings in my Xamarin.Android project.

我想在我的 Xamarin.Android 项目中保存和检索一些应用程序设置。

I know that in Android (java), I use the class SharedPreferencesto store this information, but I do not know how to convert that to Xamarin C#.

我知道在 Android (java) 中,我使用该类SharedPreferences来存储此信息,但我不知道如何将其转换为 Xamarin C#。

When I type "SharedPreferences" into the Xamarin Studio IDE, there is no auto-completion, so I don't know what to use.

当我在 Xamarin Studio IDE 中键入“SharedPreferences”时,没有自动完成功能,所以我不知道该使用什么。



An initial search of the interwebs took me to a related question, but only contains Android java:

对互联网的初步搜索将我带到了一个相关的问题,但只包含 Android java:



So to summarise:

所以总结一下:

  • What is the Xamarin Android C# equivalentof Android Java's SharedPreferences?
  • 什么是Xamarin Android C# 等价于 Android Java 的SharedPreferences

回答by Richard Le Mesurier

The Xamarin.Android equivalent of SharedPreferencesis an interface called ISharedPreferences.

Xamarin.Android 的等效项SharedPreferences是一个名为ISharedPreferences.

Use it in the same way, and you will be able to easily port Android code across.

以同样的方式使用它,您将能够轻松地移植 Android 代码。



For example, to save a true/false boolusing some Contextyou can do the following:

例如,要bool使用 some保存真/假,Context您可以执行以下操作:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (mContext);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("key_for_my_bool_value", mBool);
// editor.Commit();    // applies changes synchronously on older APIs
editor.Apply();        // applies changes asynchronously on newer APIs

Access saved values using:

使用以下方法访问保存的值:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (mContext);
mBool = prefs.GetBoolean ("key_for_my_bool_value", <default value>);
mInt = prefs.GetInt ("key_for_my_int_value", <default value>);
mString = prefs.GetString ("key_for_my_string_value", <default value>);


From this sample, you can see that once you know the correct C# interface to use, the rest is easy. There are many Android java examples on how to use SharedPreferencesfor more complex situations, and these can be ported very easily using ISharedPreferences.

从这个示例中,您可以看到,一旦您知道要使用的正确 C# 接口,剩下的就很简单了。有很多关于如何在SharedPreferences更复杂的情况下使用的 Android java 示例,这些可以很容易地使用ISharedPreferences.

For more information, read this thread:

有关更多信息,请阅读此线程:

回答by Miguel Rodríguez

You can use this example for you SharedPreferences in Xamarin.Android

您可以在 Xamarin.Android 中为您使用此示例 SharedPreferences

First, you need to use :

首先,您需要使用:

ISharedPreferences //Interface for accessing and modifying preference data
ISharedPreferencesEditor // Interface used for modifying values in a ISharedPreferences

You can create a similar class

您可以创建一个类似的类

public class AppPreferences
{
    private ISharedPreferences mSharedPrefs;
    private ISharedPreferencesEditor mPrefsEditor;
    private Context mContext;

    private static String PREFERENCE_ACCESS_KEY = "PREFERENCE_ACCESS_KEY";

    public AppPreferences (Context context)
    {
        this.mContext = context;
        mSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
        mPrefsEditor = mSharedPrefs.Edit ();            
    }

    public void saveAccessKey(string key){
        mPrefsEditor.PutString(PREFERENCE_ACCESS_KEY, key);
        mPrefsEditor.Commit();
    }

    public string getAccessKey(){
        return mSharedPrefs.GetString(PREFERENCE_ACCESS_KEY, "");
    }
}

From the Activity.

从活动。

Context mContext = Android.App.Application.Context;
AppPreferences ap = new AppPreferences (mContext);

If you want to save some value:

如果你想保存一些值:

string key = "123123";
ap.saveAccessKey (key);

If you want to get the value

如果你想获得价值

string key = ap.getAccessKey();

I hope this example helps you

我希望这个例子对你有帮助

回答by Yohan Dahmani

I had trouble using PreferenceManager as the example shows. I added this code at the top and now I'm good using it.

如示例所示,我在使用 PreferenceManager 时遇到了麻烦。我在顶部添加了这段代码,现在我可以很好地使用它。

using Android.Preferences;

Plus to get the preferences you have to add the default value as a second parameter or it will not compile

另外要获得首选项,您必须添加默认值作为第二个参数,否则将无法编译

mInt = prefs.GetInt ("key_for_my_int_value", defaultInt);

回答by Nevaran

Not sure if you still dont know or not, but make sure you Disposeyour variables if they are inside a function:

不确定您是否仍然不知道,但请确保您处理变量,如果它们在函数内:

prefs.Dispose();
prefEditor.Dispose();

I had a crash/freeze on my app over some time because of not disposing the memory whenever it is not needed anymore.

一段时间以来,我的应用程序崩溃/冻结,因为不再需要时不再处理内存。