Sterling数据库不能在Windows手机上保留

时间:2012-03-22 22:10:51

标签: windows-phone-7 sterling-db

我跟随了几个人的英镑数据库示例。它们似乎都不适合我。当我在我的数据库中保留一些东西时,一切都清楚地在调试时使用英镑(在我的手机上,而不是模拟器)持久化。但是,当我重新启动我的应用程序时,数据库是空的。是否有其他人遇到同样的问题。或者有人有一个完整的工作示例。我知道我的序列化和保存工作...只要我不重新启动我的应用程序加载我的州工作......

我的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.
    }

1 个答案:

答案 0 :(得分:4)

默认的Sterling数据库使用内存驱动程序。要坚持,请将其传递给隔离的存储驱动程序。根据文档指南快速入门:

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

代码如下所示:

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

请注意传入隔离存储驱动程序的实例。这应该为您完成。

如有疑问,请查看源附带的单元测试。这些包含大量的内存,隔离存储等示例,以显示各种设置模式。