尝试从Azure easy表中获取时,SyncContext尚未初始化错误

时间:2017-10-27 20:35:20

标签: c# azure uwp

我创建了一个UWP应用程序,它使用IMobileServiceSyncTable从一个简单的表中提取数据,但当我尝试使用它从表中获取信息放入列表时,我得到一个SyncContext尚未初始化错误。我可以将数据添加到数据库中,只是收到给我带来麻烦的东西。

继承我与表格交互的代码:

private MobileServiceCollection<DrillItem, DrillItem> drills;
    private IMobileServiceSyncTable<DrillItem> drillTable = App.MobileService.GetSyncTable<DrillItem>();

    public CombatDrillsTable()
    {

    }

    public MobileServiceCollection<DrillItem, DrillItem> GetDrills()
    {
        return this.drills;
    }

    public async Task AddDrill(DrillItem drillItem, String n, int s, int t, string sty)
    {
        drillItem.Name = n;
        drillItem.Sets = s;
        drillItem.SetTime = t;
        drillItem.Style = sty;

        await App.MobileService.GetTable<DrillItem>().InsertAsync(drillItem);
        drills.Add(drillItem);
    }

    public async void GetById(string n)
    {
        IMobileServiceTableQuery<DrillItem> query = drillTable.Where(drillItem => drillItem.Name == n)
            .Select(drillItem => drillItem);
        List<DrillItem> items = await query.ToListAsync();
        Console.WriteLine(items);
    }

    public async Task GetDrillsAsync(String cat)
    {
        MobileServiceInvalidOperationException exception = null;
        try { 
        drills = await drillTable.Where(drillItem => drillItem.Style == cat)
                .ToCollectionAsync();
            Console.WriteLine(drills);
        }
        catch (MobileServiceInvalidOperationException e)
        {
            exception = e;
        }
        if (exception != null)
        {
            await new MessageDialog(exception.Message, "Error loading items").ShowAsync();
        }
        else
        {
            // code here
        }

    }

继承激活get:

的代码
    String parameters;
    CombatTableView ctv = new CombatTableView();
    private ObservableCollection<DrillItem> _items;
    private ObservableCollection<DrillItem> _temp;

    public DisplayDrills()
    {
        this.InitializeComponent();
        _items = new ObservableCollection<DrillItem>();
        AddItemsAsync();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        parameters = (String)e.Parameter;

        testBox.Text = parameters;


        RefreshListView.ItemsSource = _items;
    }

    private async Task updateDrillsAsync(){
        await ctv.combatDrillsTable.GetDrillsAsync(parameters);
    }

    private async void AddItemsAsync()
    {

        await updateDrillsAsync();
        _temp = ctv.combatDrillsTable.GetDrills();

        foreach (var t in _temp)
        {
            _items.Insert(0, t);
        }
    }

1 个答案:

答案 0 :(得分:0)

如上所述,我们必须先初始化本地SQLite数据库,然后才能使用离线客户端。详情请参阅Configuring the Local SQLite database。例如:

private async Task InitLocalStoreAsync()
{
   if (!App.MobileService.SyncContext.IsInitialized)
   {
       var store = new MobileServiceSQLiteStore("localstore.db");
       store.DefineTable<TodoItem>();
       await App.MobileService.SyncContext.InitializeAsync(store);
   }

   await SyncAsync();
}

private async Task SyncAsync()
{
   await App.MobileService.SyncContext.PushAsync();
   await todoTable.PullAsync("todoItems", todoTable.CreateQuery());
}

执行此操作的最佳位置是GetTable<>方法,以下代码显示了OnNavigatedTo时初始化它的示例,:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{ 
   await InitLocalStoreAsync(); // offline sync
   ButtonRefresh_Click(this, null);
}