Windows Phone 7上的Sterling序列化问题

时间:2011-09-19 08:11:12

标签: windows-phone-7 sterling-db

我的Sterling Database for Windows Phone存在问题。我在wp7app中逐步实现了数据库,但是在保存新实体时它没有序列化我的数据。例如:我使用英镑数据库序列化凭证:

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

但是当重新激活(或重新启动)应用程序时,Sterling不会从隔离存储中返回任何值:

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

My ActivateEngine方法看起来是标准的,TableDefinition是:

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

为什么英镑数据库不会序列化我的数据?一切似乎都很好。请帮忙。

1 个答案:

答案 0 :(得分: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>