2016-08-27 10 views
0

私はXamarinフォームに関する質問があります。ポータブルライブラリXamarin Formsプロジェクト内のリソースの相対パスを知る方法はありますか?

私は最初のクロスプラットフォームアプリケーション(iOSとAndroid)を開発しています。私はForm Appソリューションを作成することを選択しました。

私のアプリケーションは、アプリケーションがFTPサーバーから起動するときにダウンロードされるSQLiteデータベースを使用する必要があります。 サーバにアクセスできない場合、アプリケーションはポータブルライブラリに置いたローカルSQLiteデータベースを使用する必要があります。

これを実行するには、ポータブルライブラリ内にフォルダを作成し、SQLiteファイルを内部に配置しました。 (これは正しいのですか、またはiOS/Androidプロジェクトに入れる必要がありますか?)

このローカルデータベースへの接続文字列を取得するには、そのパスを知る必要があります。どのように私は知っていますか?

ありがとうございました!

public interface ISQLite 
{ 
    SQLiteConnection GetConnection(); 
} 

と各プロジェクトにこのインタフェースを実装しています、あなたは以下のように各デバイス上のパスに応じて、接続を取得する唯一の方法一度持っています共有プロジェクトにインターフェイスを実装しなければならないことを行うには

答えて

0

IOSため

[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(); 
    } 
} 

}

0アンドロイド-for

}

関連する問題