LocalSettings和后台任务WP 8.1

时间:2015-03-22 16:16:13

标签: windows-phone-8 local-storage background-task

如何使用后台任务更改Windows.Storage.ApplicationData.Current.LocalSettings中的值。我使用像后台任务这样的代码:

namespace MainTask
{
    public sealed class Task :IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
            var storage = Windows.Storage.ApplicationData.Current.LocalSettings;
            int i = (int)storage.Values["var"];
            i++;

            storage.Values["val"] = i;

            _deferral.Complete();
        }
    }
}

后台任务已启动,调试器中存在实时循环事件,它会读取存储。但是值[“val”]不会改变。

1 个答案:

答案 0 :(得分:0)

namespace MainTask
{
    public sealed class Task :IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
            var storage = Windows.Storage.ApplicationData.Current.LocalSettings;
            int i = (int)storage.Values["var"];
            i++;

            storage.Values.Remove("val");
            storage.Values.Add("val", i);

            _deferral.Complete();
        }
    }
}