Azure脱机同步 - 未捕获的异常

时间:2017-05-23 23:54:49

标签: azure xamarin xamarin.forms offline-caching

我正在使用Xamarin Forms和Azure,尝试在我的移动应用中实现离线同步,以存储Machine个对象的列表。

在我的处理离线同步的课程中,我有一个名为Initialize()的功能,用于在手机或平板电脑中设置本地数据库,以进行离线同步

    public MobileServiceClient client { get; set; }
    IMobileServiceSyncTable<Machine> machineTable;

    public async Task Initialize()
    {
        if (client?.SyncContext?.IsInitialized ?? false)
        {
            return;
        }
        var azureUrl = "http://mycoolwebsite.azurewebsites.net";

        //Create our client
        client = new MobileServiceClient(azureUrl);

        //InitialzeDatabase for path
        var path = "mylocaldb.db";
        path = Path.Combine(MobileServiceClient.DefaultDatabasePath, path);

        //setup our local sqlite store and intialize our table
        var store = new MobileServiceSQLiteStore(path);

        //Define table
        store.DefineTable<Machine>();

        //Initialize SyncContext -- Populate the local DB!
        await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());  //<-- EXCEPTION HERE
        machineTable = client.GetSyncTable<Machine>(); 
        // table.PurgeAsync();
    }

当我运行此代码时,我在上面突出显示的行上遇到异常。我错过了什么?作为参考,我正在关注this教程。

1 个答案:

答案 0 :(得分:2)

事实证明,使用时:

    await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
    machineTable = client.GetSyncTable<Machine>(); 

我需要在public string Id { get; set; }对象中有一个Machine变量,因为构建本地数据库的代码需要此字段。如果将其添加到某处的文档中会很好!