class - Xamarin - 必须定义数据库路径
问题描述
根据我找到的教程,我在 MainActivity.cs 中声明了 db3 文件的 android 路径,如下所示:
string fileName = "galleries.db3";
string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string completePath = Path.Combine(folderPath, fileName);
LoadApplication(new App(completePath));
在 App.xaml.cs 我添加了这个:
public App(string filePath)
{
InitializeComponent();
MainPage = new NavigationPage(new GalleryList());
FilePath = filePath;
}
我想在一个类中访问这个路径,如下所示:
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
但是得到我必须指定路径的错误消息,并且变量为 Null。如何使该路径在类中可用?
解决方案
您在分配之前正在执行您的数据库代码(在GalleryList
) FilePath
MainPage = new NavigationPage(new GalleryList());
FilePath = filePath;
相反,FilePath
首先分配
FilePath = filePath;
MainPage = new NavigationPage(new GalleryList());
推荐阅读
- javascript - 失败的道具类型:提供给“字段”的道具“组件”无效。安装 mui v4 后
- redisgraph - RedisGraph:指定整数类型的值?最大整数?
- .net - 从 MVC2 升级到 MVC5 项目
- c# - 使用 MyString.Remove(x,y) 从 C# 中的字符串中删除多个字符
- java - 就地合并排序
- javascript - 如果将属性添加到冻结的全局对象,则抛出错误?
- python - 在熊猫数据透视表中运行 sum (python)
- java - 无法自动接线。找不到“RoleMappingService”类型的 bean
- javascript - 如何定义独特的按钮类型?
- python - read_only 但仍填充在序列化程序的 create 方法中