示例#1
0
        private async Task LoadAsync()
        {
            Folder = ApplicationData.Current.LocalCacheFolder;

            try
            {
                JsonFile = await Folder.GetFileAsync(JsonFileName);
            }
            catch (FileNotFoundException)
            {
                try
                {
                    var oldPinnedItemsFile = await Folder.GetFileAsync("PinnedItems.txt");

                    var oldPinnedItems = await FileIO.ReadLinesAsync(oldPinnedItemsFile);

                    await oldPinnedItemsFile.DeleteAsync();

                    foreach (var line in oldPinnedItems)
                    {
                        if (!Model.FavoriteItems.Contains(line))
                        {
                            Model.FavoriteItems.Add(line);
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    Model.AddDefaultItems();
                }

                JsonFile = await Folder.CreateFileAsync(JsonFileName, CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(JsonFile, JsonConvert.SerializeObject(Model, Formatting.Indented));
            }

            try
            {
                Model = JsonConvert.DeserializeObject <SidebarPinnedModel>(await FileIO.ReadTextAsync(JsonFile));
                if (Model == null)
                {
                    throw new Exception($"{JsonFileName} is empty, regenerating...");
                }
                Model.SetController(this);
            }
            catch (Exception)
            {
                await JsonFile.DeleteAsync();

                Model = new SidebarPinnedModel();
                Model.SetController(this);
                Model.AddDefaultItems();
                Model.Save();
            }

            await Model.AddAllItemsToSidebar();
        }
        private async Task LoadAsync()
        {
            StorageFolder Folder = await FilesystemTasks.Wrap(() => ApplicationData.Current.LocalFolder.CreateFolderAsync("settings", CreationCollisionOption.OpenIfExists).AsTask());

            if (Folder == null)
            {
                Model.AddDefaultItems();
                await Model.AddAllItemsToSidebar();

                return;
            }

            var JsonFile = await FilesystemTasks.Wrap(() => Folder.GetFileAsync(JsonFileName).AsTask());

            if (!JsonFile)
            {
                if (JsonFile == FileSystemStatusCode.NotFound)
                {
                    var oldItems = await ReadV2PinnedItemsFile() ?? await ReadV1PinnedItemsFile();

                    if (oldItems != null)
                    {
                        foreach (var item in oldItems)
                        {
                            if (!Model.FavoriteItems.Contains(item))
                            {
                                Model.FavoriteItems.Add(item);
                            }
                        }
                    }
                    else
                    {
                        Model.AddDefaultItems();
                    }

                    Model.Save();
                    await Model.AddAllItemsToSidebar();

                    return;
                }
                else
                {
                    Model.AddDefaultItems();
                    await Model.AddAllItemsToSidebar();

                    return;
                }
            }

            try
            {
                configContent = await FileIO.ReadTextAsync(JsonFile.Result);

                Model = JsonConvert.DeserializeObject <SidebarPinnedModel>(configContent);
                if (Model == null)
                {
                    throw new ArgumentException($"{JsonFileName} is empty, regenerating...");
                }
                Model.SetController(this);
            }
            catch (Exception)
            {
                Model = new SidebarPinnedModel();
                Model.SetController(this);
                Model.AddDefaultItems();
                Model.Save();
            }

            await Model.AddAllItemsToSidebar();
        }