Class Xamarin-必须定义数据库的路径
根据我找到的教程,我在MainActivity.cs中声明了android到db3文件的路径,如下所示:Class Xamarin-必须定义数据库的路径,class,xamarin,global-variables,availability,Class,Xamarin,Global Variables,Availability,根据我找到的教程,我在MainActivity.cs中声明了android到db3文件的路径,如下所示: string fileName = "galleries.db3"; string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); string completePath = Path.Combine(folder
string fileName = "galleries.db3";
string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string completePath = Path.Combine(folderPath, fileName);
LoadApplication(new App(completePath));
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
在App.xaml.cs中,我添加了以下内容:
public App(string filePath)
{
InitializeComponent();
MainPage = new NavigationPage(new GalleryList());
FilePath = filePath;
}
我希望在类中访问此路径,如下所示:
string fileName = "galleries.db3";
string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string completePath = Path.Combine(folderPath, fileName);
LoadApplication(new App(completePath));
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
但得到的错误消息是,我必须指定一个路径,并且变量为Null。
如何使该路径在类中可用?在分配
文件路径之前,您正在执行数据库代码(在GalleryList
中)
MainPage = new NavigationPage(new GalleryList());
FilePath = filePath;
而是首先分配FilePath
FilePath = filePath;
MainPage = new NavigationPage(new GalleryList());
“我发现的教程”-你考虑过遵循官方文件吗?谢谢,看一下。我已经查过了,但仍然不知道如何将路径传递给我的班级。如果我在链接中这样做,那么我将不得不以某种方式将数据库实例传递给我的类,并且将在同一点上。。对吗?创建数据库时,您可以在共享代码中执行此操作。你不需要从平台项目中传递它。对不起,我只是刚刚开始,不确定我是否理解你。我试图做的是:我有一个CollectionView和一个“服务”类,它获取所有数据并将它们传递给ViewModel。在这个服务类中,我试图连接到数据库以读取所有需要的数据,但是我无法告诉数据库的路径。我尝试使用示例中的代码,但由于公共函数的原因,它会出现问题。