3

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

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

1 回答 1

4

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

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

代码如下所示:

_databaseInstance = _engine.SterlingDatabase.RegisterDatabase(新独立存储驱动程序());

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

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

于 2012-03-23T13:21:11.157 回答