我的应用程序中有多个 ObservableCollections,我想保存、编辑等等。但是由于它们是我自己类型的类的集合,SQLite 无法识别它们。保存论文的最佳方法是什么?或者我做错了什么。
有点像这样:
在我的应用程序中:(当然里面还有更多代码(
public partial class App : Application
{
static MyDatabase database;
public static MyDatabase Database
{
get
{
if (database == null)
{
database = new MyDatabase(DependencyService.Get
<IFileHelper>().GetLocalFilePath("MyListSQLite.db3"));
}
return database;
}
}
}
我的数据库:
public class MyDatabase
{
readonly SQLiteAsyncConnection database;
public MyDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<MyClass>().Wait();
}
public Task<List<MyClass>> GetItemsAsync()
{
return database.Table<MyClass>().ToListAsync();
}
public Task<MyClass> GetItemAsync(int id)
{
return database.Table<MyClass>().Where(i => i.ID == id).FirstOrDefaultAsync();
}
public Task<int> SaveItemAsync(MyClass item)
{
if (item.ID != 0)
{
return database.UpdateAsync(item);
}
else
{
return database.InsertAsync(item);
}
}
public Task<int> DeleteItemAsync(MyClass item)
{
return database.DeleteAsync(item);
}
}
我的班级:
public class MyClass : ObservableCollection<MyOtherClass>
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
}
我的其他类:
public class MyOtherClass : BindableObject
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public static readonly BindableProperty ListNbrProperty =
BindableProperty.Create("ListNbr", typeof(int), typeof(MyOtherClass), default(int));
public int ListNbr
{
get { return (int)GetValue(ListNbrProperty); }
set { SetValue(ListNbrProperty, value); }
}
public bool IsMaster { get; set; }
public static readonly BindableProperty ResizedPathProperty =
BindableProperty.Create("ResizedPath", typeof(String), typeof(MyOtherClass), default(String));
public String ResizedPath
{
get { return (String)GetValue(ResizedPathProperty); }
set { SetValue(ResizedPathProperty, value); }
}
...
}
在我们的大多数应用程序中,对于复杂对象,我们只需将它们序列化为 Json,以便它们可以存储为字符串(在您的情况下为 NVarChar(MAX( ?(。我们并不总是使用SQL,有时我们使用原生项目自己的存储机制(即iOS的NSUserDefaults(
using Newtonsoft.Json;
public static async Task<bool> CacheList (ObservableList<string> MyList)
{
try
{
var json = JsonConvert.SerializeObject(MyList);
// you dont need to encrypt the string, but we deal with some
// sensitive data so we have a seperate cryptography class that handles this.
var EncryptedJson = Cryptography.Crypto.Encrypt(json, Key);
// Now you have your serialised object simply store it into your database using whatever method you usually user
[Store to SQLdatabase Code goes here]
return await Task.FromResult(true);
}
catch (Exception genEx)
{
var Error = genEx.Message;
return await Task.FromResult(false);
}
}
并检索它:
public static async Task<ObservableList<string>> RetrieveList (string key)
{
try
{
// Retrieve your json string from your sql database
[retrieve from SQLdatabase Code goes here - returns EncryptedJson]
// In our case we decrypt it
var DecryptedJson = Cryptography.Crypto.Decrypt(EncryptedJson, Key);
var DeSerialisedData = JsonConvert.DeserializeObject<ObservableList<string>>(DecryptedJson);
return await Task.FromResult(DeSerialisedData ?? new ObservableList<string>());
}
catch (Exception genEx)
{
var Error = genEx.Message;
return await Task.FromResult(new ObservableList<string>());
}
}
以上不是复制和粘贴,它将工作示例,它实际上更像是我们在应用程序中使用的高级概念。