【问题标题】:how can I know relative path of a resource inside of a portable library Xamarin Forms project?如何知道可移植库 Xamarin Forms 项目中资源的相对路径?
【发布时间】:2017-01-04 01:04:33
【问题描述】:

我有一个关于 Xamarin 表单的问题。

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

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

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

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

谢谢!

【问题讨论】:

    标签: android sqlite xamarin xamarin.ios content-type


    【解决方案1】:

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

    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
    

    并在每个项目上实现此接口 - 对于 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
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多