在Windows商店应用中,Deployment.Current.Dispatcher.BeginInvoke是如何工作的?

3

我有使用Windows Phone 8的经验,并且正在使用WCF数据服务。我能够成功地通过以下代码更新我的记录:

public void UpdateJob1(EquipBooking equipBooking)
        {
            this._context.UpdateObject(equipBooking);
            this._context.BeginSaveChanges(OnChangesSaved, this._context);
        }

        private void OnChangesSaved(IAsyncResult result)
        {
            bool errorFound = false;
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                this._context = result.AsyncState as THA001_devEntities;

                try
                {
                    // Complete the save changes operation.
                    this._context.EndSaveChanges(result);
                }
                catch (DataServiceRequestException ex)
                {
                    errorFound = true;
                    MessageBox.Show("Error, While Updating Record");
                }

                if (!errorFound)
                {
                    MessageBox.Show("Record Successfully Updated");
                }
            }
            );

        }

但是当我在Windows Store应用程序中编写相同的代码时,我遇到了问题。 我无法更新记录,我在这里得到了问题:Deployment.Current.Dispatcher.BeginInvoke

有人可以指导我或重写我的代码吗?

谢谢

2个回答

9
你尝试过使用这个而不是Deployment.Current.Dispatcher.BeginInvoke吗?它们都可以用来在UI线程中执行操作。
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
      {

      });

编辑:

整个方法如下:

private async void OnChangesSaved(IAsyncResult result)
{
    bool errorFound = false;
    CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
      {
        this._context = result.AsyncState as THA001_devEntities;
        try
        {
            // Complete the save changes operation.
            this._context.EndSaveChanges(result);
        }
        catch (DataServiceRequestException ex)
        {
            errorFound = true;
            MessageBox.Show("Error, While Updating Record");
        }

        if (!errorFound)
        {
            MessageBox.Show("Record Successfully Updated");
        }
      });
}

我猜,我不知道这个,你能重写我的代码吗? - patel
@vb1,这里的方法已经被重写了。 - Igor Ralic
我添加了一个答案,如果它与你的一样,那么我会标记你的答案为最终答案。 - patel
我得到了以下错误信息:System.InvalidOperationException: 找不到当前的窗口对象以获取 CoreDispatcher。或者 CoreWindow.GetForCurrentThread() 为空。 - Agent_L

1

我重新编写了这段内容,请审核语法。

public void ModfityJobs(EquipBooking equipBooking)
        {
            try
            {
                this.IsDataLoaded = true;
                _context.BeginSaveChanges(ModfityJobsAsynchCallBack, equipBooking);
            }
            catch (Exception ex)
            {
            }
        }

        private void ModfityJobsAsynchCallBack(IAsyncResult synchresult)
        {
            try
            {
                dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    {
                        _context.EndSaveChanges(synchresult);
                    });
            }
            catch (Exception)
            {

                throw;
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接