ApplicationData.LocalSettings不存储数据

本文关键字:数据 存储 LocalSettings ApplicationData | 更新日期: 2023-09-27 18:19:21

我正在制作一个应用程序,其中用户输入两次值(starthour, startminute, endhour, endminute)。我编写了一个函数,用于保存值,然后检查值并将值放入文本框中。然而,它不工作,我不知道为什么。我想这是我的一个错误,但我也不能完全肯定。下面是代码:

        public async Task savedata()
    {
        while (true)
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            localSettings.Values["starthour1"] = starthour1.Text;
            localSettings.Values["starthour2"] = starthour2.Text;
            localSettings.Values["starthour3"] = starthour3.Text;
            localSettings.Values["starthour4"] = starthour4.Text;
            localSettings.Values["starthour5"] = starthour5.Text;
            localSettings.Values["starthour6"] = starthour6.Text;
            localSettings.Values["starthour7"] = starthour7.Text;
            localSettings.Values["startminute1"] = startminute1.Text;
            localSettings.Values["startminute2"] = startminute2.Text;
            localSettings.Values["startminute3"] = startminute3.Text;
            localSettings.Values["startminute4"] = startminute4.Text;
            localSettings.Values["startminute5"] = startminute5.Text;
            localSettings.Values["startminute6"] = startminute6.Text;
            localSettings.Values["startminute7"] = startminute7.Text;
            localSettings.Values["endhour1"] = endhour1.Text;
            localSettings.Values["endhour2"] = endhour2.Text;
            localSettings.Values["endhour3"] = endhour3.Text;
            localSettings.Values["endhour4"] = endhour4.Text;
            localSettings.Values["endhour5"] = endhour5.Text;
            localSettings.Values["endhour6"] = endhour6.Text;
            localSettings.Values["endhour7"] = endhour7.Text;
            localSettings.Values["endminute1"] = endminute1.Text;
            localSettings.Values["endminute2"] = endminute2.Text;
            localSettings.Values["endminute3"] = endminute3.Text;
            localSettings.Values["endminute4"] = endminute4.Text;
            localSettings.Values["endminute5"] = endminute5.Text;
            localSettings.Values["endminute6"] = endminute6.Text;
            localSettings.Values["endminute7"] = endminute7.Text;
            //get data
            Object starthour1o = localSettings.Values["starthour1"];
            if (starthour1o == null)
            {
                // No data
            }
            else
            {
                starthour1.Text = starthour1o.ToString();
            }
            Object starthour2o = localSettings.Values["starthour2"];
            if (starthour2o == null)
            {
                // No data
            }
            else
            {
                starthour2.Text = starthour2o.ToString();
            }
            Object starthour3o = localSettings.Values["starthour3"];
            if (starthour3o == null)
            {
                // No data
            }
            else
            {
                starthour3.Text = starthour3o.ToString();
            }
            Object starthour4o = localSettings.Values["starthour4"];
            if (starthour4o == null)
            {
                // No data
            }
            else
            {
                starthour4.Text = starthour4o.ToString();
            }
            Object starthour5o = localSettings.Values["starthour5"];
            if (starthour5o == null)
            {
                // No data
            }
            else
            {
                starthour5.Text = starthour5o.ToString();
            }
            Object starthour6o = localSettings.Values["starthour6"];
            if (starthour6o == null)
            {
                // No data
            }
            else
            {
                starthour6.Text = starthour6o.ToString();
            }
            Object starthour7o = localSettings.Values["starthour7"];
            if (starthour7o == null)
            {
                // No data
            }
            else
            {
                starthour7.Text = starthour7o.ToString();
            }
            await Task.Delay(10);
        }
    }

ApplicationData.LocalSettings不存储数据

您需要做两件事,首先您需要通过调用save()显式地保存您的设置以便持久化它们。在你代码的某个地方,你需要做localSettings.Save(),它应该工作。

第二,如果你保存了设置,你的代码所做的第一件事就是用文本框的当前值覆盖它们,整个顶部的localSettings.Values["Foo"] = Foo.Text需要移到底部。

作为旁注,你真的需要每10毫秒更新一次你的代码吗?这将在你的程序中消耗大量的资源。一种更正常的方法是在启动时加载值,然后在关机时保存它们。