MvvmCross SQLite 插件不适用于 UWP 项目
MvvmCross SQLite Plugin not working for UWP Project
当我尝试 运行 我的 UWP 项目时出现以下错误:
MvvmCross.Platform.Exceptions.MvxIoCResolveException: 'Failed to
resolve parameter for parameter factory of type
IMvxSqliteConnectionFactory when creating
DebtBuddy.Core.Repositories.AccountRepository'
我的 android 项目 运行 没有问题。以下是我的仓库 class.
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 包添加到您的 UWP 项目?由于该插件在两个平台上都受支持,这很可能是您的项目在一个平台上失败而在另一个平台上运行的最可能原因。
此外,请注意 MvvmCross SQLite 插件是 deprecated,应避免使用。
您应该停止使用它,因为 MvvmCross SQLite 插件已被弃用。我还建议使用 SQLiteAsyncConnection
将所有操作包装在 Task
中,类似于您在此处所做的。
最近首选的 SQLite 包称为 sqlite-net-pcl
,可在 NuGet and GitHub 上获得。此版本的库支持 Android Nougat 及更高版本,并在最新版本中以 .Net Standard 为目标。
MvvmCross SQLite 包装器只是 SQLite 的一个较小的包装器。自己重现 MvvmCross SQLite 插件很容易。这是一个这样的例子:
将此接口放入您的 PCL/.Net Standard "Core" 项目中:
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 "Core" 项目中的 ViewModel、存储库等共享 ISqliteConnectionService
。
当我尝试 运行 我的 UWP 项目时出现以下错误:
MvvmCross.Platform.Exceptions.MvxIoCResolveException: 'Failed to resolve parameter for parameter factory of type IMvxSqliteConnectionFactory when creating DebtBuddy.Core.Repositories.AccountRepository'
我的 android 项目 运行 没有问题。以下是我的仓库 class.
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 包添加到您的 UWP 项目?由于该插件在两个平台上都受支持,这很可能是您的项目在一个平台上失败而在另一个平台上运行的最可能原因。
此外,请注意 MvvmCross SQLite 插件是 deprecated,应避免使用。
您应该停止使用它,因为 MvvmCross SQLite 插件已被弃用。我还建议使用 SQLiteAsyncConnection
将所有操作包装在 Task
中,类似于您在此处所做的。
最近首选的 SQLite 包称为 sqlite-net-pcl
,可在 NuGet and GitHub 上获得。此版本的库支持 Android Nougat 及更高版本,并在最新版本中以 .Net Standard 为目标。
MvvmCross SQLite 包装器只是 SQLite 的一个较小的包装器。自己重现 MvvmCross SQLite 插件很容易。这是一个这样的例子:
将此接口放入您的 PCL/.Net Standard "Core" 项目中:
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 "Core" 项目中的 ViewModel、存储库等共享 ISqliteConnectionService
。