如何在app暂停和启动时保存并加载observablecollection?

时间:2016-05-03 02:50:44

标签: c# win-universal-app

我正在尝试弄清楚当应用程序关闭或暂停时如何保存observablecollection项目,然后在再次打开应用程序时重新加载它们。这就是我所拥有的,但由于这些方法是静态的,并且是从App.xaml.cs文件调用的,因此MemoryItems observablecollection需要引用。所以,我真的不明白如何保存和更新observablecollection。 我试图在App.xaml.cs文件中使用ViewModel的一个实例,但后来我在MemoryItems observablecollection中得到零项。

function update_quantity() {
  $(this).val(function(_, val) {
      return val.length === 1 && val[0] === "-" 
             ? val[0] + val.slice(1).replace(/[^\d+]/g, "")
             : val.replace(/(-)$/g, "")
  });
}
$(document).on("change keyup", "input[type=text]", update_quantity);

然后在App.xaml.cs文件中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<input type="text" name="quantity">

1 个答案:

答案 0 :(得分:0)

我的解决方案是删除静态关键字并使用To和From OnNavigatedAsync方法处理viewmodel中的所有内容。我不希望每次应用程序启动时都会恢复大量数据,这是有效的,因此尽管它不够优雅,但它可以完成工作。

        private bool newPage = true;

        public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending)
    {
        await Serializer();
    }

    public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
    {
        if (newPage == true) 
        {
            await Deserializer();
            newPage = false;
        }
    }


public async Task Serializer()
    {
        var folder = ApplicationData.Current.RoamingFolder;
        var file = await folder.CreateFileAsync("collection.json", CreationCollisionOption.ReplaceExisting);
        using (var stream = await file.OpenStreamForWriteAsync())
        using (var writer = new StreamWriter(stream, Encoding.UTF8))
        {
            string json = JsonConvert.SerializeObject(MemoryItems);
            await writer.WriteAsync(json);
        }
    }
    public async Task Deserializer()
    {
        try
        {
            var folder = ApplicationData.Current.RoamingFolder;
            var file = await folder.GetFileAsync("collection.json");
            using (var stream = await file.OpenStreamForReadAsync())
            using (var reader = new StreamReader(stream, Encoding.UTF8))
            {
                var json = await reader.ReadToEndAsync();
                ObservableCollection<MemoryItem> MemoryItems = JsonConvert.DeserializeObject<ObservableCollection<MemoryItem>>(json);

                if(MemoryItems.Count != 0)
                {
                    foreach (var item in MemoryItems)
                    {
                        var sb = new StringBuilder();
                        sb.Append(item.Memory);
                        var fileString = sb.ToString();

                        var memoryItem = new MemoryItem
                        {
                            Memory = fileString
                        };
                        this.MemoryItems.Add(memoryItem);
                    }
                }
                else
                {
                    Memory();
                } 
            }
        }
        catch (Exception)
        {
            // Handle Exception.
        }
    }