b2n*_*rma 0 c# sqlite mvvmcross xamarin uwp
当我尝试运行我的 UWP 项目时出现以下错误:
MvvmCross.Platform.Exceptions.MvxIoCResolveException: '在创建 DebtBuddy.Core.Repositories.AccountRepository 时无法解析类型为 IMvxSqliteConnectionFactory 的参数工厂的参数'
我的 android 项目运行没有问题。以下是我的存储库类。
public class AccountRepository : IAccountRepository
{
private readonly SQLiteConnection _connection;
public AccountRepository(IMvxSqliteConnectionFactory factory)
{
_connection = factory.GetConnection("Account.db");
_connection.CreateTable<Account>();
}
public async Task<List<Account>> GetAllAccounts()
{
return await Task.FromResult(_connection.Table<Account>().ToList());
}
public async Task Insert(Account account)
{
await Task.Run(() => _connection.Insert(account));
}
public async void Update(Account account)
{
await Task.FromResult(_connection.Update(account));
}
public async void Delete(int id)
{
await Task.FromResult(_connection.Delete(id));
}
}
Run Code Online (Sandbox Code Playgroud)
您应该停止使用它,因为 MvvmCross SQLite 插件已被弃用。我还建议使用SQLiteAsyncConnectionwhich 将所有操作包装在Task类似于您在此处所做的操作中。
现在首选的 SQLite 包称为sqlite-net-pcl,可在NuGet和GitHub 上获得。此版本的库支持 Android Nougat 及更高版本,并针对最新版本的 .Net Standard。
MvvmCross SQLite 包装器只是围绕 SQLite 的一个较小的包装器。自己复制 MvvmCross SQLite 插件很容易。这是一个这样的例子:
将此接口放在您的 PCL/.Net 标准“核心”项目中:
public interface ISqliteConnectionService
{
SQLiteAsyncConnection GetAsyncConnection();
}
Run Code Online (Sandbox Code Playgroud)
然后为每个平台实现接口。这是 Android 的样子。抱歉,我手头没有 UWP 示例。
public class AndroidSqliteConnectionService : ISqliteConnectionService
{
private const string FileName = "File.sqlite3";
private SQLiteAsyncConnection _connection;
public SQLiteAsyncConnection GetAsyncConnection()
{
if (_connection == null)
{
var databaseFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var databaseFilePath = Path.Combine(databaseFolder, FileName);
_connection = new SQLiteAsyncConnection(databaseFilePath);
}
return _connection;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Setup.cs各个平台的 中注册实现:
protected override void InitializeFirstChance()
{
Mvx.LazyConstructAndRegisterSingleton<ISqliteConnectionService, AndroidSqliteConnectionService>();
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用构造函数依赖注入ISqliteConnectionService与您的 PCL/.Net 标准“核心”项目内的 ViewModel、存储库等共享。
| 归档时间: |
|
| 查看次数: |
537 次 |
| 最近记录: |