为什么在尝试获取SQLite数据库连接时会出现空引用异常?
我试图创建一个SQLite数据库,但遇到了一个异常。该错误无法说明异常发生的原因。它只是说一个空引用异常。 异常发生在这一行为什么在尝试获取SQLite数据库连接时会出现空引用异常?,sqlite,xamarin.forms,nullreferenceexception,Sqlite,Xamarin.forms,Nullreferenceexception,我试图创建一个SQLite数据库,但遇到了一个异常。该错误无法说明异常发生的原因。它只是说一个空引用异常。 异常发生在这一行 var connection = `DependencyService.Get<ISQLiteDB>().GetConnection();` 这是iOS和Android中的实现 using System; using System.Collections.Generic; using System.IO; using System.Linq; using S
var connection = `DependencyService.Get<ISQLiteDB>().GetConnection();`
这是iOS和Android中的实现
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App4.Persistence;
using SQLite;
namespace App4.Droid.Persistence
{
public class SQLiteDB : ISQLiteDB
{
public SQLiteAsyncConnection GetConnection()
{
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var DatabasePath = Path.Combine(documentsPath,"MySQLite.db3");
return new SQLiteAsyncConnection(DatabasePath);
}
}
}
虽然iOS和Android的实现是相同的(本例中不需要界面),但我将为windows创建需要不同实现的项目,以获取数据库路径
谢谢你们的帮助
谢谢。例外的问题是没有在实现类中定义依赖属性attrbute,我应该在命名空间定义的正上方而不是类中使用
[assembly:dependency(typeof(SQLiteDB)]
尽管在尝试通过dependency属性注册实现类时,我仍然会遇到另一个错误
我得到这个错误:
没有与形式参数对应的参数
“LoadHintArgument”或DependencyAtrrBute.DependencyAttribute(字符串
加载提示)
在此主题上的任何帮助都将不胜感激
>
> using SQLite;
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace App4.Persistence
> {
> public interface ISQLiteDB
> {
> SQLiteAsyncConnection GetConnection();
>
> }
> }
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App4.Persistence;
using SQLite;
namespace App4.Droid.Persistence
{
public class SQLiteDB : ISQLiteDB
{
public SQLiteAsyncConnection GetConnection()
{
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var DatabasePath = Path.Combine(documentsPath,"MySQLite.db3");
return new SQLiteAsyncConnection(DatabasePath);
}
}
}