简体   繁体   中英

MvvmCross SQLite Plugin not working for UWP Project

I am getting the following error when I try to run my UWP project:

MvvmCross.Platform.Exceptions.MvxIoCResolveException: 'Failed to resolve parameter for parameter factory of type IMvxSqliteConnectionFactory when creating DebtBuddy.Core.Repositories.AccountRepository'

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

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.

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.

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:

Put this interface in your PCL/.Net Standard "Core" project:

public interface ISqliteConnectionService
{
    SQLiteAsyncConnection GetAsyncConnection();
}

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

Then register the implementation in the Setup.cs of each platform:

protected override void InitializeFirstChance()
{
    Mvx.LazyConstructAndRegisterSingleton<ISqliteConnectionService, AndroidSqliteConnectionService>();
}

Now you can use constructor Dependency Injection to share the ISqliteConnectionService with your ViewModels, Repositories, etc. inside of your PCL/.Net Standard "Core" project.

Did you remember to add the MvvmCross SQLite package to your UWP project? Since the plugin is supported on both platforms, that would be the most likely reason your project is failing on one and working on the other.

Also, please note that the MvvmCross SQLite plugin is deprecated and should be avoided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM