2011-09-19 85 views
0

我在使用Windows Phone的Sterling Database时遇到问题。我在我的wp7app中一步一步地实现了数据库,但是当保存新实体时,它不会序列化我的数据。例如:我使用英镑数据库序列化的凭据:Windows Phone 7上的Sterling序列化问题

 var userCredentials = new UserCredentials(userName, password); 
     App.Database.Save(userCredentials); 
     App.Database.Flush(); 

但是,当应用程序被重新激活(或重新启动)英镑不从独立存储返回任何值:

var firstOrDefault = App.Database.Query<UserCredentials, string>() 
      .ToList() 
      .FirstOrDefault(); 

我ActivateEngine方法看起来是标准TableDefinition是:

CreateTableDefinition< UserCredentials, string >(t => t.UserName),

为什么英镑数据库不序列米y数据?一切看起来都很好。请帮忙。

回答

1

您是否正在启动并注册数据库,并在完成时按照Quickstart中的说明完成?

我个人的偏好是使用类似以下内容的应用服务:

namespace MyApp.Data 
{ 
    using System.Windows; 
    using Wintellect.Sterling; 
    using Wintellect.Sterling.IsolatedStorage; 

    /// 
    /// Defines a an application service that supports the Sterling database. 
    /// 
    public class SterlingDatabaseService : IApplicationService, IApplicationLifetimeAware 
    { 
     public static SterlingDatabaseService Current { get; private set; } 

     public ISterlingDatabaseInstance Database { get; private set; } 

     private SterlingEngine _engine; 

     /// 
     /// Called by an application in order to initialize the application extension service. 
     /// 
     /// Provides information about the application state. 
     public void StartService(ApplicationServiceContext context) 
     { 
      Current = this; 
      _engine = new SterlingEngine(); 
     } 

     /// 
     /// Called by an application in order to stop the application extension service. 
     /// 
     public void StopService() 
     { 
      _engine = null; 
     } 

     /// 
     /// Called by an application immediately before the event occurs. 
     /// 
     public void Starting() 
     { 
      _engine.Activate(); 
      Database = _engine 
       .SterlingDatabase 
       .RegisterDatabase(new IsolatedStorageDriver()); 
     } 

     /// 
     /// Called by an application immediately after the event occurs. 
     /// 
     public void Started() 
     { 
      return; 
     } 

     /// 
     /// Called by an application immediately before the event occurs. 
     /// 
     public void Exiting() 
     { 
      _engine.Dispose(); 
     } 

     /// 
     /// Called by an application immediately after the event occurs. 
     /// 
     public void Exited() 
     { 
      return; 
     } 
    } 
}

如果你使用这种方法,不要忘了在App.xaml中添加实例:

<Application.ApplicationLifetimeObjects> 
     <!-- Required object that handles lifetime events for the application. --> 
     <shell:PhoneApplicationService Activated="Application_Activated" 
             Closing="Application_Closing" 
             Deactivated="Application_Deactivated" 
             Launching="Application_Launching" /> 
     <data:SterlingDatabaseService /> 
    </Application.ApplicationLifetimeObjects> 
+0

谢谢你的发帖德里克, 它帮了我很多。我注意到当我注册数据表时,我没有指定,我想使用IsolatedStorageDriver。我用这条线固定它: _database = _engine.SterlingDatabase.RegisterDatabase< MyDatabase >(new IsolatedStorageDriver()); 非常感谢你! – xawer