如何在 LocalStorage 中保存文件?
How to save a file in LocalStorage?
我有一个 ObservableCollection <T>
。我想在其中插入各种元素,然后将新创建的文件保存在 LocalStorage
中。我该怎么做?
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db"), true);
await conn.CreateTableAsync<Musei>();
var Dbase = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db");
var con = new SQLiteAsyncConnection(Dbase, true);
var query = await con.Table<Musei>().ToListAsync();
ObservableCollection<Musei> favMusei = new ObservableCollection<Musei>();
if (query.Count > 0)
{
favMusei.Clear();
foreach (Musei museifav in query)
{
favMusei.Add(museifav);
}
}
我正在使用 json 文件存储在内存中。 JSON 是一种轻量级的消息交换格式,被广泛使用。如果你想要一些不同的文件格式,你必须对代码做一些轻微的修改。
您的集合将在保存时序列化到内存中,并且在从内存中读回时必须反序列化。
添加您自己的集合通用实现。为了创建你的情况,我使用了一个简单的 ObservableCollection<int>
。并且不要忘记将集合初始化为一些有意义的值,这里我使用默认构造函数初始化。
using System.Collections.ObjectModel;
using System.Runtime.Serialization.Json;
using Windows.Storage;
//Add your own generic implementation of the collection
//and make changes accordingly
private ObservableCollection<int> temp;
private string file = "temp.json";
private async void saveToFile()
{
//add your items to the collection
temp = new ObservableCollection<int>();
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<int>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(file, CreationCollisionOption.ReplaceExisting))
{
jsonSerializer.WriteObject(stream, temp);
}
}
private async Task getFormFile()
{
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<int>));
try
{
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(file))
{
temp = (ObservableCollection<int>)jsonSerializer.ReadObject(stream);
}
}
catch
{
//if some error is caught while reading data from the file then initializing
//the collection to default constructor instance is a good choice
//again it's your choice and may differ in your scenario
temp = new ObservableCollection<int>();
}
}
要向代码添加一些功能,您还可以使用 ensureDataLoaded()
函数来确保已从 JSON 文件中读取数据。
public async Task ensureDataLoaded()
{
if (temp.Count == 0)
await getFormFile();
return;
}
在使用全局变量temp
(具有ObservableCollection
)之前调用ensureDataLoaded
函数。它会避免一些不必要的 NullPointerExceptions
.
我有一个 ObservableCollection <T>
。我想在其中插入各种元素,然后将新创建的文件保存在 LocalStorage
中。我该怎么做?
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db"), true);
await conn.CreateTableAsync<Musei>();
var Dbase = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Database.db");
var con = new SQLiteAsyncConnection(Dbase, true);
var query = await con.Table<Musei>().ToListAsync();
ObservableCollection<Musei> favMusei = new ObservableCollection<Musei>();
if (query.Count > 0)
{
favMusei.Clear();
foreach (Musei museifav in query)
{
favMusei.Add(museifav);
}
}
我正在使用 json 文件存储在内存中。 JSON 是一种轻量级的消息交换格式,被广泛使用。如果你想要一些不同的文件格式,你必须对代码做一些轻微的修改。
您的集合将在保存时序列化到内存中,并且在从内存中读回时必须反序列化。
添加您自己的集合通用实现。为了创建你的情况,我使用了一个简单的 ObservableCollection<int>
。并且不要忘记将集合初始化为一些有意义的值,这里我使用默认构造函数初始化。
using System.Collections.ObjectModel;
using System.Runtime.Serialization.Json;
using Windows.Storage;
//Add your own generic implementation of the collection
//and make changes accordingly
private ObservableCollection<int> temp;
private string file = "temp.json";
private async void saveToFile()
{
//add your items to the collection
temp = new ObservableCollection<int>();
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<int>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(file, CreationCollisionOption.ReplaceExisting))
{
jsonSerializer.WriteObject(stream, temp);
}
}
private async Task getFormFile()
{
var jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<int>));
try
{
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(file))
{
temp = (ObservableCollection<int>)jsonSerializer.ReadObject(stream);
}
}
catch
{
//if some error is caught while reading data from the file then initializing
//the collection to default constructor instance is a good choice
//again it's your choice and may differ in your scenario
temp = new ObservableCollection<int>();
}
}
要向代码添加一些功能,您还可以使用 ensureDataLoaded()
函数来确保已从 JSON 文件中读取数据。
public async Task ensureDataLoaded()
{
if (temp.Count == 0)
await getFormFile();
return;
}
在使用全局变量temp
(具有ObservableCollection
)之前调用ensureDataLoaded
函数。它会避免一些不必要的 NullPointerExceptions
.