Windows phone 7 Sterling数据库在Windows phone上不持久

Windows phone 7 Sterling数据库在Windows phone上不持久,windows-phone-7,sterling-db,Windows Phone 7,Sterling Db,我跟随了几个人的sterling数据库示例。他们两个似乎都不适合我。当我在数据库中保存一些东西时,在调试时,使用sterling(在我的手机上,而不是在模拟器上)显然可以保存所有东西。但是,当我重新启动我的应用程序时,数据库是空的。其他人也有同样的问题。或者某人有完整的工作示例。我知道我的序列化和保存工作。。。只要我不重新启动我的应用程序加载我的状态工作 我的app.cs中的代码 public static ISterlingDatabaseInstance Database { get

我跟随了几个人的sterling数据库示例。他们两个似乎都不适合我。当我在数据库中保存一些东西时,在调试时,使用sterling(在我的手机上,而不是在模拟器上)显然可以保存所有东西。但是,当我重新启动我的应用程序时,数据库是空的。其他人也有同样的问题。或者某人有完整的工作示例。我知道我的序列化和保存工作。。。只要我不重新启动我的应用程序加载我的状态工作

我的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;
    }
public静态数据库实例数据库{get;private set;}
私人静态斯特林发动机;
专用静态记录器\u记录器;
私有void应用程序\u启动(对象发送方,启动事件参数e)
{
激活引擎();
}
//激活应用程序时要执行的代码(带到前台)
//此代码在应用程序首次启动时不会执行
私有无效应用程序\u已激活(对象发送器,激活的事件目标)
{
激活引擎();
}
//停用应用程序时要执行的代码(发送到后台)
//当应用程序关闭时,此代码将不会执行
私有无效应用程序\u已停用(对象发送方,已停用目标)
{
去激活引擎();
}
//应用程序关闭时要执行的代码(例如,用户回击)
//当应用程序被停用时,此代码将不会执行
私有作废应用程序\u关闭(对象发送方,ClosingEventArgs e)
{
去激活引擎();
}
私有无效激活引擎()
{
_发动机=新斯特林发动机();
_记录器=新的SterlingDefaultLogger(SterlingLogLevel.Information);
_引擎。激活();
数据库=_engine.SterlingDatabase.RegisterDatabase();
}
私有无效停用引擎()
{
_logger.Detach();
_引擎。Dispose();
数据库=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.
    }
public void LoadState(整数级)
{
var levelState=App.Database.Load(级别);
如果(levelState!=null)
{
//TODO:检查游戏是否开始,然后从boardstring属性创建棋盘,否则创建新棋盘
//Labyrint=newlabyrint(Factory.CreateBoard());
新游戏(关卡);
}
其他的
{
新游戏(关卡);
}
}
public void SaveState()
{
var-levelState=new-levelState{LevelId=\u-level,Moves=Labyrint.Moves,Board=Labyrint.ToString()};
App.Database.Save(levelState);
App.Database.Flush();//需要清除索引等。
}

默认的Sterling数据库使用内存中的驱动程序。要持久化,请向其传递一个独立的存储驱动程序。根据《文档指南快速入门》:

代码如下所示:

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

请注意传入的独立存储驱动程序的实例。那应该对你有用


如果有疑问,请查看源代码附带的单元测试。这些包含大量的内存、独立存储等示例,展示了各种设置模式。

您知道自Mango以来就存在内置数据库吗?是的,我知道,但我不需要像SQL CEThanks这样的关系数据库,Jeremy!我正准备写这个确切的问题。忘了看单元测试——至少可以节省我一个小时