DomainCollectionView不更改页面

时间:2014-02-07 21:22:24

标签: silverlight-5.0 wcf-ria-services

我正在尝试从服务器下载大量项目并避免超时我正在使用分页,所以我尝试做的是加载第一页并在我的DomainCollectionView Loaded方法中调用MoveToNextPage等等直到所有页面装了。

当我这样做时,DCV不会从页面改变,它仍然在第0页,然后我尝试刷新DCV的PadeChanged事件,但在那里,当我刷新加载操作时,PageIndex是-1 abd取消。

这是我的代码:

private void OnLoadedPrintingModels(LoadOperation<AppointmentModel> op)
    {
        if (op.HasError)
        {
            MessageBox.Show(ApplicationStrings.PrintingConnectionError + Environment.NewLine + op.Error.Message);
            op.MarkErrorAsHandled();
            this.downloadDialog.Close();
            this.TotalPrintCountD = 100.0;
            this.downloadDialog = null;
            this.printingList.Clear();
            this.printingDomainList.Source = null;
        }
        else if (!op.IsCanceled)
        {
            if (op.Entities.Any())
            {
                printingList.AddRange(op.Entities);
                if (this.isStartingPrinting)
                {
                    this.TotalPrintCount = op.TotalEntityCount;
                    this.TotalPrintCountD = (double)op.TotalEntityCount;
                    this.isStartingPrinting = false;
                }
                isDownloaded = true;
                printingDomainList.Source = op.Entities;
                printingDomainView.SetTotalItemCount(op.TotalEntityCount);

            }
            else
            {
                MessageBox.Show(ApplicationStrings.NoSearchResults);
                isStartingPrinting = true;
                this.downloadDialog.Close();
                this.TotalPrintCountD = 100.0;
                this.downloadDialog = null;
                this.printingList.Clear();
                this.printingDomainList.Source = null;
                DownloadedPrintingItems = 0.0;
            }

        }
        else
        {
            MessageBox.Show(ApplicationStrings.PrintOperationCanceledByUser);
            this.downloadPrintDataCanceled = false;
            this.downloadDialog.Close();
            this.TotalPrintCountD = 100.0;
            this.downloadDialog = null;
            this.printingList.Clear();
            this.printingDomainList.Source = null;
            isStartingPrinting = true;
            DownloadedPrintingItems = 0.0;
        }
    }
    bool isDownloaded = false;
    void printingDomainView_PageChanged(object sender, EventArgs e)
    {
        if (isDownloaded)
        {
            isDownloaded = false;
            DownloadedPrintingItems = 100.0 * (double)printingList.Count / this.TotalPrintCountD;

            if (printingList.Count < this.TotalPrintCount)
            {
                using (this.printingDomainView.DeferRefresh())
                {
                    this.printingDomainView.PageSize = PrintingPageSize;
                    this.printingDomainView.MoveToNextPage();
                }
            }
            else
            {
                isStartingPrinting = true;
                this.downloadDialog.Close();
                this.downloadDialog = null;
                DownloadedPrintingItems = 0.0;
                ConfirmPrintingResults();
            }

        }
    }

我正在尝试做很多事情,但似乎没有任何作用。如何做任何帮助将非常感激。

非常感谢你。

1 个答案:

答案 0 :(得分:0)

我终于通过使用async和await等待一个线程内的DomainCollectionView.IsChangingPage解决了这个问题,结果代码如下:

private **async** void OnLoadedPrintingModels(LoadOperation<AppointmentModel> op)
    {
        if (op.HasError)
        {
            MessageBox.Show(ApplicationStrings.PrintingConnectionError + Environment.NewLine + op.Error.Message);
            op.MarkErrorAsHandled();
            this.downloadDialog.Close();
            this.TotalPrintCountD = 100.0;
            this.downloadDialog = null;
            this.printingList.Clear();
            this.printingDomainList.Source = null;
        }
        else if (!op.IsCanceled)
        {
            if (op.Entities.Any())
            {
                printingList.AddRange(op.Entities);
                if (this.isStartingPrinting)
                {
                    this.TotalPrintCount = op.TotalEntityCount;
                    this.TotalPrintCountD = (double)op.TotalEntityCount;
                    this.isStartingPrinting = false;
                }
                printingDomainList.Source = op.Entities;
                printingDomainView.SetTotalItemCount(op.TotalEntityCount);

                **await Task.Factory.StartNew(() =>
                    {
                        while (printingDomainView.IsPageChanging)
                        {
                            Thread.Sleep(1);
                        }
                    });**

                DownloadedPrintingItems = 100.0 * (double)printingList.Count / this.TotalPrintCountD;

                if (printingList.Count < this.TotalPrintCount)
                {
                    using (this.printingDomainView.DeferRefresh())
                    {
                        this.printingDomainView.PageSize = PrintingPageSize;
                        this.printingDomainView.MoveToNextPage();
                    }
                }
                else
                {
                    //this.IsBusy = false;
                    //this.WaitingMessage = ApplicationStrings.WaitLabel;
                    isStartingPrinting = true;
                    this.downloadDialog.Close();
                    //this.TotalPrintCountD = 100.0;
                    this.downloadDialog = null;
                    DownloadedPrintingItems = 0.0;
                    //Thread.CurrentThread.Join(1);
                    ConfirmPrintingResults();
                }

            }
            else
            {
                MessageBox.Show(ApplicationStrings.NoSearchResults);
                isStartingPrinting = true;
                this.downloadDialog.Close();
                this.TotalPrintCountD = 100.0;
                this.downloadDialog = null;
                this.printingList.Clear();
                this.printingDomainList.Source = null;
                DownloadedPrintingItems = 0.0;
            }

        }
        else
        {
            MessageBox.Show(ApplicationStrings.PrintOperationCanceledByUser);
            this.downloadPrintDataCanceled = false;
            this.downloadDialog.Close();
            this.TotalPrintCountD = 100.0;
            this.downloadDialog = null;
            this.printingList.Clear();
            this.printingDomainList.Source = null;
            isStartingPrinting = true;
            DownloadedPrintingItems = 0.0;
        }
    }