MvvmCross SQLite插件不适用于UWP项目 [英] MvvmCross SQLite Plugin not working for UWP Project
问题描述
当我尝试运行我的UWP项目时出现以下错误:
I am getting the following error when I try to run my UWP project:
MvvmCross.Platform.Exceptions.MvxIoCResolveException:'失败 为类型的参数工厂解析参数 创建时IMvxSqliteConnectionFactory DebtBuddy.Core.Repositories.AccountRepository'
MvvmCross.Platform.Exceptions.MvxIoCResolveException: 'Failed to resolve parameter for parameter factory of type IMvxSqliteConnectionFactory when creating DebtBuddy.Core.Repositories.AccountRepository'
我的android项目运行没有问题.以下是我的存储库类.
My android project runs with no problems. The following is my repository 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插件,因此您应该远离使用它.我还建议您使用SQLiteAsyncConnection
将所有操作包装在Task
内,类似于您在此处所做的操作.
You should move away from using this since the MvvmCross SQLite Plugin was deprecated. I'd also recommend using SQLiteAsyncConnection
which wraps all the operations inside a Task
similar to what you did here.
如今,首选的SQLite软件包称为sqlite-net-pcl
,可在 NuGet中获得和 GitHub .该版本的库支持Android Nougat和更高版本,并且以最新版本的.Net Standard为目标.
The preferred SQLite package these days is called sqlite-net-pcl
, which is available on NuGet and GitHub. This version of the library supports Android Nougat and later, and targets .Net Standard in the most recent versions.
MvvmCross SQLite包装器只是围绕SQLite的较小包装器.自己复制MvvmCross SQLite插件很容易.这是一个这样的例子:
The MvvmCross SQLite wrapper was just a smaller wrapper around SQLite. It's easy to reproduce the MvvmCross SQLite Plugin on your own. Here is one such example:
将此接口放在您的PCL/.Net Standard核心"项目中:
Put this interface in your PCL/.Net Standard "Core" project:
public interface ISqliteConnectionService
{
SQLiteAsyncConnection GetAsyncConnection();
}
然后为每个平台实现接口.这是Android的外观.抱歉,我手边没有UWP示例.
Then you implement the interface for each platform. Here is what it would look like for Android. I'm sorry, I don't have a UWP example on hand.
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
中注册实现:
Then register the implementation in the Setup.cs
of each platform:
protected override void InitializeFirstChance()
{
Mvx.LazyConstructAndRegisterSingleton<ISqliteConnectionService, AndroidSqliteConnectionService>();
}
现在,您可以使用构造函数依赖注入在PCL/.Net Standard核心"项目中与ViewModel,存储库等共享ISqliteConnectionService
.
Now you can use constructor Dependency Injection to share the ISqliteConnectionService
with your ViewModels, Repositories, etc. inside of your PCL/.Net Standard "Core" project.
这篇关于MvvmCross SQLite插件不适用于UWP项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!