Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当应用程序即将关闭时,如何在SharedReferences c#中保存int?_C#_Android_Xamarin - Fatal编程技术网

当应用程序即将关闭时,如何在SharedReferences c#中保存int?

当应用程序即将关闭时,如何在SharedReferences c#中保存int?,c#,android,xamarin,C#,Android,Xamarin,我想在应用程序关闭时保存SharedReferences中的点击次数。现在我有一个按钮连接的功能 private void SaveClicks (){ var prefs = Application.Context.GetSharedPreferences("Name",FileCreationMode.Private); var prefEditor = prefs.Edit(); prefEditor.Put

我想在应用程序关闭时保存SharedReferences中的点击次数。现在我有一个按钮连接的功能

   private void SaveClicks (){
    var prefs = Application.Context.GetSharedPreferences("Name",FileCreationMode.Private);
                 var prefEditor = prefs.Edit();
                 prefEditor.PutInt("Key", nametest);
                 prefEditor.Apply();

    }
“clicks”是int的名称,其中我在其中一个按钮中存储单击次数

当应用程序关闭时,我可以用什么方式自动完成?使用onDestroy将是一个很好的解决方案

//更新 所以我写了这段代码:

protected override void OnDestroy()
        {
            var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
            var prefEditor = prefs.Edit(); // 2
            prefEditor.PutInt("Key", nametest); // 3
            prefEditor.Apply(); // 4


        }
为了计算点击次数,我有类似的东西

var prefs = Application.Context.GetSharedPreferences("Name", FileCreationMode.Private); // 1
            var value1 = prefs.GetInt("key", 0);
            if (clicks + value1 <= 499)
            {
                clicks++;
                textViewBattlepackCount.Text = (clicks + value1).ToString() + " clicks!";
                progressBarName1.Progress = progressBarName1.Progress + 1;
                nametest= clicks + value1;


                if (clicks + value1 == 500)
                {
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                    alertDialog.SetTitle("You won!");
                    alertDialog.SetMessage("message");
                    alertDialog.SetNeutralButton("Ok", delegate
                    {

                        alertDialog.Dispose();

                    });
                    alertDialog.Show();

                    clicks = 0;
                    nametest= 0;

                    textViewTXTCount.Text = "0";
                    progressBarName1.Progress = progressBarName1.Progress = 0;

                }
var prefs=Application.Context.getSharedReferences(“Name”,FileCreationMode.Private);//1.
var value1=prefs.GetInt(“键”,0);

如果(单击+值1,这取决于您的要求:

  • 如果您只想存储活动终止时的单击次数

  • 如果您想在活动处于后台时存储单击次数(例如,您按下“主页”按钮或其他活动已启动),请使用onPause()

  • onSaveInstanceState()也可以在活动销毁之前调用,它可以还原一些临时数据(例如EditText中的文本)


  • 关于你的下一个问题

    调用onDestory()时应包含SharedReferences“key”,如果不包含,请使用debug跟踪“nametest”的值

    顺便说一句


    请使用相同的“键”--!!

    取决于您对“关闭”的含义。是当它在屏幕上不再可见时,还是当应用程序被Android关闭时?太好了!谢谢帮助。:)我可以再向您寻求帮助吗?我刚刚编辑了我的问题。哦,再次谢谢你,伙计。
      prefEditor.PutInt("Key", nametest);
      var value1 = prefs.GetInt("key", 0);