首页 > 解决方案 > 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。如何使该路径在类中可用?

标签: classxamaringlobal-variablesavailability

解决方案


您在分配之前正在执行您的数据库代码(在GalleryListFilePath

MainPage = new NavigationPage(new GalleryList());
FilePath = filePath;

相反,FilePath首先分配

FilePath = filePath;
MainPage = new NavigationPage(new GalleryList());

推荐阅读