2016-08-27 2 views
0

J'ai une question à propos de Xamarin Forms.comment puis-je connaître le chemin relatif d'une ressource à l'intérieur d'un projet de bibliothèque portable Xamarin Forms?

Je développe ma première application multiplateforme (iOS et Android) et j'ai choisi de créer une solution Form App.

Mon application doit utiliser une base de données SQLite qui est téléchargée lorsque l'application démarre à partir d'un serveur FTP. Si le serveur n'est pas accessible, l'application doit utiliser une base de données SQLite locale que j'ai mise dans la bibliothèque portable. Pour cela, j'ai créé un dossier dans la bibliothèque portable et j'ai placé les fichiers SQLite à l'intérieur. (Est-ce correct ou dois-je mettre dans le projet iOS/Android?)

Pour avoir la chaîne de connexion à cette base de données locale, j'ai besoin de connaître son chemin. Comment puis-je savoir?

merci!

Répondre

0

de le faire, vous mettrez en oeuvre l'interface sur le projet partagé qui aura qu'une seule fois méthode pour récupérer la connexion en fonction du parcours de chaque appareil comme ci-dessous:

public interface ISQLite 
{ 
    SQLiteConnection GetConnection(); 
} 

et mettre en œuvre cette interface sur chaque projet -Pour 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(); 
    } 
} 

}

pour 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 
} 

}