gpt4 book ai didi

database - 将 ObservableCollection 保存到 SQLite 或类似的 Xamarin Forms

转载 作者:搜寻专家 更新时间:2023-10-30 23:34:13 24 4
gpt4 key购买 nike

我的应用程序中有多个 ObservableCollections,我想保存、编辑等。但由于它们是我自己的类的集合,SQLite 无法识别它们。保存论文的最佳方法是什么?或者我做错了什么。

有点像这样:

在我的App中:(当然里面还有更多的代码)

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);
}

}

我的类(class):

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)

使用 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>());
}
}

以上不是复制和粘贴,它可以作为示例,它实际上是我们在应用程序中使用的更高级的概念。

关于database - 将 ObservableCollection<MyClass> 保存到 SQLite 或类似的 Xamarin Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45191853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com