0

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

1 回答 1

0

Two things you need to do, first you need to explicitly save your settings for them to be persisted by calling Save(). Somewhere in your code you need to do localSettings.Save() and it should work.

2nd, if you have saved settings the first thing your code does is overwrite them with the current values of the text boxes, the whole top section where it is localSettings.Values["Foo"] = Foo.Text needs to be moved to the bottom.

As a side comment, do you really need to be updating your code every 10 miliseconds? That is going to eat up a TON of resources in your program. A much more normal approach is load the values at start-up then save them at shutdown.

于 2013-06-21T18:43:51.973 回答