Xamarin sqliteAsyncConnection无法打开数据库文件

时间:2018-10-06 20:43:00

标签: android ios xamarin sqlite-net

我正在使用Visual Studio for Mac。我在预览器中收到一个异常错误,指出

  

“呈现控件时发生异常”。

当我单击感叹号以了解详细信息时

  

SQLite.SqlLiteException无法打开数据库文件。

我找不到任何问题。它在模拟器中运行良好,但在预览器中不起作用。

SQLiteConnection:

 public class SQLiteDb : ISOLiteDb
{
    public SQLiteAsyncConnection GetConnection()
    {
        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var path = Path.Combine(documentsPath, "MySQLite.db3"); 
       var conn = new SQLiteAsyncConnection(path);
       return conn;

    }

响应:

public class UserService
{
    private readonly SQLiteAsyncConnection _connection;
    public UserService()
    {
        _connection = DependencyService.Get<ISOLiteDb>().GetConnection();

    }

    public async Task AddUser(User user)
    {
        await _connection.CreateTableAsync<User>();
        await _connection.InsertAsync(user);

    }

viewModel:

  private UserService _userService;
    private readonly IPageService _iPageService;

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }

    public SignOnViewModel(IPageService pageService)
    {
        _userService = new UserService();
        _iPageService = pageService;
    }

后面的代码:

 public partial class SignOnPage : ContentPage
{
    public SignOnPage()
    {
        BindingContext = new SignOnViewModel(new PageService());
        InitializeComponent();
    }

    void CreateAccount(object sender, System.EventArgs e)
    {
        (BindingContext as SignOnViewModel).AddUser();
    }

0 个答案:

没有答案