[英]MvvmCross SQLite Plugin not working for UWP Project
当我尝试运行我的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));
}
}
由于不建议使用MvvmCross SQLite插件,因此您应该远离使用它。 我还建议使用SQLiteAsyncConnection
,它将所有操作包装在Task
类似于您在此处所做的操作。
sqlite-net-pcl
,首选的SQLite软件包称为sqlite-net-pcl
,可在NuGet和GitHub上找到 。 该版本的库支持Android Nougat和更高版本,并且以.Net Standard为最新版本。
MvvmCross SQLite包装器只是SQLite的较小包装器。 自己复制MvvmCross SQLite插件很容易。 这是一个这样的例子:
将此接口放在您的PCL / .Net Standard“核心”项目中:
public interface ISqliteConnectionService
{
SQLiteAsyncConnection GetAsyncConnection();
}
然后为每个平台实现接口。 这是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;
}
}
然后在每个平台的Setup.cs
中注册实现:
protected override void InitializeFirstChance()
{
Mvx.LazyConstructAndRegisterSingleton<ISqliteConnectionService, AndroidSqliteConnectionService>();
}
现在,您可以使用构造函数依赖注入在PCL / .Net Standard“核心”项目中与ViewModels,存储库等共享ISqliteConnectionService
。
您还记得将MvvmCross SQLite软件包添加到UWP项目吗? 由于两个平台都支持该插件,因此这很可能是您的项目在一个平台上失败而在另一个平台上工作的原因。
另外,请注意, 不建议使用 MvvmCross SQLite插件,应避免使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.