2013-10-29 37 views
0

使用实体框架从数据库加载数据需要很长时间我想在UI(WPF)上显示“加载”指示器。对于指标本身,我使用文章中所示的WPF Loading Wait Adorner实体框架加载数据时WPF加载指示器

该指标工作正常,但在Entity Framework加载数据时未显示。在这种情况下,指标在用户界面上根本不会显示。

我运行此:

'show Adorner (loading indicator) 
LoadingAdorner.IsAdornerVisible = Not LoadingAdorner.IsAdornerVisible 

'read data from database with Entity Framework 
Persons = _context.persons 

'hide Adorner (loading indicator) after loading data is completed 
LoadingAdorner.IsAdornerVisible = Not LoadingAdorner.IsAdornerVisible 

<ac:AdornedControl Name="LoadingAdorner"> 
     <ac:AdornedControl.AdornerContent> 
      <local:LoadingWait></local:LoadingWait> 
     </ac:AdornedControl.AdornerContent> 
     <ListBox> 
      ...code not shown 
     </ListBox> 
</ac:AdornedControl> 

加载数据只有经过时,指示灯变为可见。 我错过了什么,以及如何显示数据加载指标?

回答

2

问题是您正在主线程中运行EF调用。这会阻止UI被更新,直到您从数据库接收到所有数据。
为了解决这个问题只需要添加BackgroundWorker或异步方法:

var worker = new BackgroundWorker(); 
    worker.DoWork += (s, e) => { 
     this.IsLoading = true; 
     this.Persons = _context.persons; 
    };   
    worker.RunWorkerCompleted += (s, e) => { 
     this.IsLoading = false; 
    }; 

重要:请记住跨线程访问(在后台线程执行DoWork的完成 - UI线程)

最后开始/触发DoWork您需要在您的员工上执行.RunWorkerAsync()

+0

我该如何使用它?我现在怎么对它说“doWork”? –

+0

@PieroAlberto'.RunWorkerAsync();'将开始工作。 –