如何知道可移植库 Xamarin Forms 项目中资源的相对路径?

how can I know relative path of a resource inside of a portable library Xamarin Forms project?

提问人:Felice Lombardi 提问时间:8/27/2016 更新时间:8/27/2016 访问量:913

问:

我有一个关于 Xamarin Forms 的问题。

我正在开发我的第一个跨平台应用程序(iOS 和 Android),我选择创建一个 Form App 解决方案。

我的应用程序需要使用SQLite数据库,该数据库是在应用程序从FTP服务器启动时下载的。 如果无法访问服务器,则应用程序应使用我放在可移植库中的本地 SQLite 数据库。

为此,我在可移植库中创建了一个文件夹,并将SQLite文件放入其中。(这是正确的吗?或者我需要放入iOS / Android项目?

若要获得此本地数据库的连接字符串,我需要知道其路径。我怎么知道?

谢谢!

Android SQLite Xamarin Xamarin.iOS 内容类型

评论


答:

0赞 MohamedHamza 8/27/2016 #1

为此,您应该在共享项目上实现接口,该接口将只有一个方法来检索连接,具体取决于每个设备上的路径,如下所示:

public interface ISQLite
{
    SQLiteConnection GetConnection();
}

并在每个项目上实现此接口 - for Android:

[assembly: Dependency (typeof (SQLite_Android))]

namespace GenYouth.Droid
{
    public class SQLite_Android : ISQLite
    {
        public SQLite_Android ()
        {
        }

        #region ISQLite implementation
        public SQLite.SQLiteConnection GetConnection ()
        {
            var sqliteFilename = "db_Name.db3";
            string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal); // Documents folder
            var path = Path.Combine(documentsPath, sqliteFilename);

            // This is where we copy in the prepopulated database
            Console.WriteLine (path);
            if (!File.Exists(path))
            {
            /*
            var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.GenYouth_db);  // RESOURCE NAME ###

            // create a write stream
            FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            // write to the stream
            ReadWriteStream(s, writeStream);
             * */
        }

        var conn = new SQLite.SQLiteConnection(path);

        // Return the database connection 
        return conn;
    }
    #endregion

    /// <summary>
    /// helper method to get the database out of /raw/ and into the user filesystem
    /// </summary>
    void ReadWriteStream(Stream readStream, Stream writeStream)
    {
        int Length = 256;
        Byte[] buffer = new Byte[Length];
        int bytesRead = readStream.Read(buffer, 0, Length);
        // write the required bytes
        while (bytesRead > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
            bytesRead = readStream.Read(buffer, 0, Length);
        }
        readStream.Close();
        writeStream.Close();
    }
}

}

对于IOS:

     [assembly: Dependency (typeof (SQLite_iOS))]

namespace GenYouth.iOS
{
    public class SQLite_iOS : ISQLite
    {
        public SQLite_iOS ()
        {
        }

        #region ISQLite implementation
        public SQLite.SQLiteConnection GetConnection ()
        {
            var sqliteFilename = "db_Name.db3";
            string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
            string libraryPath = Path.Combine (documentsPath, "..", "Library"); // Library folder
            var path = Path.Combine(libraryPath, sqliteFilename);

            // This is where we copy in the prepopulated database
            Console.WriteLine (path);
            if (!File.Exists (path)) {
                File.Copy (sqliteFilename, path);
            }

        var conn = new SQLite.SQLiteConnection(path);

        // Return the database connection 
        return conn;
    }
    #endregion
}

}