使用Mvvm异步/等待访问UI线程
我想使用Mvvm模式在DocumentViewer上传递一些内容,并使用进度指示器,这一代将在异步从db获取数据后使用UiElements使用Mvvm异步/等待访问UI线程,mvvm,Mvvm,我想使用Mvvm模式在DocumentViewer上传递一些内容,并使用进度指示器,这一代将在异步从db获取数据后使用UiElements public async void ProcessReportAsync(){ IsBusy = true; _reportDal = new ReportDal(_sprocName,_sprocParams); ReportContainers = new ObservableCollectio
public async void ProcessReportAsync(){
IsBusy = true;
_reportDal = new ReportDal(_sprocName,_sprocParams);
ReportContainers = new ObservableCollection<ReportContainerViewModel>();
await Task.Run(() => _reportDal.InitReportDal());
ReportDataTable = _reportDal.DataTableReport;
await Task.Run(() => ProcessedElements());
var t3 = Task.Run(() => ProcessPage(_reportPage));
var t4 = Task.Run(() => ProcessContainerData());
await Task.WhenAll(t3, t4);
var p = new PrinterViewModel(this);
// This statement does'nt complete its execuation, which is adding more UIElements
if(DispatcherHelper.UIDispatcher.CheckAccess()) {
DispatcherHelper.UIDispatcher.Invoke(
()=>_document = p.CreateDocument(new Size(p.PrintDialog.PrintableAreaWidth,p.PrintDialog.PrintableAreaHeight))
,DispatcherPriority.Background);
}
// Can't reach this code
IsBusy = false;
}
public async void ProcessReportAsync(){
IsBusy=true;
_reportDal=新的reportDal(_-sprocName,_-sprocParams);
ReportContainers=新的ObservableCollection();
等待任务。运行(()=>_reportDal.InitReportDal());
ReportDataTable=\u reportDal.DataTableReport;
等待任务。运行(()=>ProcessedElements());
var t3=Task.Run(()=>ProcessPage(_reportPage));
var t4=Task.Run(()=>ProcessContainerData());
等待任务时(t3,t4);
var p=新的PrinterViewModel(此);
//此语句未完成其执行,这将添加更多元素
if(DispatcherHelper.UIDispatcher.CheckAccess()){
DispatcherHelper.UIDispatcher.Invoke(
()=>_document=p.CreateDocument(新大小(p.PrintDialog.PrintableAreaWidth,p.PrintDialog.PrintableAreaHeight))
,DispatcherPriority.Background);
}
//无法访问此代码
IsBusy=false;
}
async
/wait
的一个很好的方面是,它负责为您发送回正确的上下文
public async Task ProcessReportAsync()
{
IsBusy = true;
_reportDal = new ReportDal(_sprocName,_sprocParams);
ReportContainers = new ObservableCollection<ReportContainerViewModel>();
await Task.Run(() => _reportDal.InitReportDal());
ReportDataTable = _reportDal.DataTableReport;
await Task.Run(() => ProcessedElements());
var t3 = Task.Run(() => ProcessPage(_reportPage));
var t4 = Task.Run(() => ProcessContainerData());
await Task.WhenAll(t3, t4);
var p = new PrinterViewModel(this);
_document = p.CreateDocument(new Size(p.PrintDialog.PrintableAreaWidth,p.PrintDialog.PrintableAreaHeight));
IsBusy = false;
}
公共异步任务ProcessReportAsync()
{
IsBusy=true;
_reportDal=新的reportDal(_-sprocName,_-sprocParams);
ReportContainers=新的ObservableCollection();
等待任务。运行(()=>_reportDal.InitReportDal());
ReportDataTable=\u reportDal.DataTableReport;
等待任务。运行(()=>ProcessedElements());
var t3=Task.Run(()=>ProcessPage(_reportPage));
var t4=Task.Run(()=>ProcessContainerData());
等待任务时(t3,t4);
var p=新的PrinterViewModel(此);
_document=p.CreateDocument(新大小(p.PrintDialog.PrintableAreaWidth,p.PrintDialog.PrintableAreaHeight));
IsBusy=false;
}
我建议您阅读my和my。异步/
等待
的一个很好的方面是,它负责为您发送回正确的上下文
public async Task ProcessReportAsync()
{
IsBusy = true;
_reportDal = new ReportDal(_sprocName,_sprocParams);
ReportContainers = new ObservableCollection<ReportContainerViewModel>();
await Task.Run(() => _reportDal.InitReportDal());
ReportDataTable = _reportDal.DataTableReport;
await Task.Run(() => ProcessedElements());
var t3 = Task.Run(() => ProcessPage(_reportPage));
var t4 = Task.Run(() => ProcessContainerData());
await Task.WhenAll(t3, t4);
var p = new PrinterViewModel(this);
_document = p.CreateDocument(new Size(p.PrintDialog.PrintableAreaWidth,p.PrintDialog.PrintableAreaHeight));
IsBusy = false;
}
公共异步任务ProcessReportAsync()
{
IsBusy=true;
_reportDal=新的reportDal(_-sprocName,_-sprocParams);
ReportContainers=新的ObservableCollection();
等待任务。运行(()=>_reportDal.InitReportDal());
ReportDataTable=\u reportDal.DataTableReport;
等待任务。运行(()=>ProcessedElements());
var t3=Task.Run(()=>ProcessPage(_reportPage));
var t4=Task.Run(()=>ProcessContainerData());
等待任务时(t3,t4);
var p=新的PrinterViewModel(此);
_document=p.CreateDocument(新大小(p.PrintDialog.PrintableAreaWidth,p.PrintDialog.PrintableAreaHeight));
IsBusy=false;
}
我建议您阅读我的和我的。非常感谢Stephen,DocumentViewer的内容现在已显示。但是IsBusy道具工作不好,Ui似乎没有同步响应和运行。我会读你的文章。非常感谢Stephen,DocumentViewer的内容现在显示出来了。但是IsBusy道具工作不好,Ui似乎没有同步响应和运行。我会读你的文章。