2012-03-22 69 views
3

我跟蹤了幾個人的英鎊數據庫示例。他們兩個似乎都不適合我。當我在數據庫中保留一些東西時,在調試時,使用英鎊(在我的手機上,而不是仿真器上)顯然都會持續使用。但是,當我重新啓動我的應用程序時,數據庫是空的。有人遇到同樣的問題。或者有人有一個完整的工作示例。我知道我的序列化和節省作品......只要我不重新啓動我的應用程序加載我的狀態工程...英鎊數據庫不會在Windows電話上持續存在

代碼在我app.cs

public static ISterlingDatabaseInstance Database { get; private set; } 
    private static SterlingEngine _engine; 
    private static SterlingDefaultLogger _logger; 

    private void Application_Launching(object sender, LaunchingEventArgs e) 
    { 
     ActivateEngine(); 
    } 

    // Code to execute when the application is activated (brought to foreground) 
    // This code will not execute when the application is first launched 
    private void Application_Activated(object sender, ActivatedEventArgs e) 
    { 
     ActivateEngine(); 
    } 

    // Code to execute when the application is deactivated (sent to background) 
    // This code will not execute when the application is closing 
    private void Application_Deactivated(object sender, DeactivatedEventArgs e) 
    { 
     DeactivateEngine(); 
    } 

    // Code to execute when the application is closing (eg, user hit Back) 
    // This code will not execute when the application is deactivated 
    private void Application_Closing(object sender, ClosingEventArgs e) 
    { 
     DeactivateEngine(); 
    } 



    private void ActivateEngine() 
    { 
     _engine = new SterlingEngine(); 
     _logger = new SterlingDefaultLogger(SterlingLogLevel.Information); 
     _engine.Activate(); 
     Database = _engine.SterlingDatabase.RegisterDatabase<SokobanDb>(); 
    } 

    private void DeactivateEngine() 
    { 
     _logger.Detach(); 
     _engine.Dispose(); 
     Database = null; 
     _engine = null; 
    } 

代碼在我的ViewModel

public void LoadState(int level) 
    { 
     var levelState = App.Database.Load<LevelState>(level); 
     if (levelState != null) 
     { 
      //TODO: check if game started, then create board from boardstring property else create new board 
      //Labyrint = new Labyrint(Factory.CreateBoard()); 
      NewGame(level); 
     } 
     else 
     { 
      NewGame(level); 
     } 
    } 

    public void SaveState() 
    { 
     var levelState = new LevelState { LevelId = _level, Moves = Labyrint.Moves, Board = Labyrint.ToString() }; 
     App.Database.Save(levelState); 
     App.Database.Flush(); //Required to clean indexes etc. 
    } 
+0

您是否知道自芒果以來存在內置數據庫? – 2012-03-22 22:42:50

+0

是的我知道,但我不需要關係數據庫像SQL CE – 2012-03-23 14:11:37

回答

4

默認Sterling數據庫使用內存中的驅動程序。堅持下去,傳遞一個孤立的存儲驅動程序。每文檔指南快速:

https://sites.google.com/site/sterlingdatabase/sterling-user-guide/getting-started

的代碼看起來是這樣的:

_databaseInstance = _engine.SterlingDatabase.RegisterDatabase(新IsolatedStorageDriver());

請注意傳入的隔離存儲驅動程序的實例。這應該爲您做。

如有疑問,請查看源代碼隨附的單元測試。這些包含大量的內存,獨立存儲等示例,以顯示設置它的各種模式。

+0

謝謝,傑里米!我只是寫了一個確切的問題。忘了看單元測試 - 至少會節省一個小時。 :) – 2012-05-09 04:35:12