0

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

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

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

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

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

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

为什么 sterling 数据库不序列化我的数据?一切似乎都执行得很好。请帮忙。

4

1 回答 1

1

您是否按照快速入门中的说明在启动时激活和注册数据库并在完成时处理?

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

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>
于 2011-09-19T08:42:52.970 回答