ApplicationData.LocalSettings不存储数据?

时间:2013-06-21 18:33:53

标签: c# windows

我正在创建一个应用程序,其中一个用户输入值两次(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);
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要做的两件事情,首先您需要通过调用Save()明确保存设置以保持它们的持久性。在代码的某处,您需要执行localSettings.Save(),它应该可以正常工作。

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

作为旁注,您是否真的需要每10毫秒更新一次代码?这会占用你程序中的大量资源。更常见的方法是在启动时加载值,然后在关闭时保存它们。