EF LoadAsync后刷新视图

本文关键字:新视图 刷新 LoadAsync EF | 更新日期: 2023-09-27 18:15:16

假设要加载大量数据,我希望UI在加载数据时响应。目前唯一有效的代码是下面的不断地刷新UI,这是不希望的。如何在非ui线程中加载数据并获得视图上的最终更新?

private static object sync_lock = new object();
private void Load()
{
    MyEntities db = new MyEntities();
    TestEntityViewModel testEntityViewModel = (TestEntityViewModel)FindResource("testEntityViewModel");
    testEntityViewModel.Source = db.TestEntities.Local; // Source is ObservableCollection<TestEntity>
    BindingOperations.EnableCollectionSynchronization(testEntityViewModel.Source, sync_lock);
            db.TestEntities.LoadAsync().ContinueWith(new Action<Task>(
                (t) =>
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        View.MoveCurrentToFirst();
                        CommandManager.InvalidateRequerySuggested();
                    }));
                }));
}

注意:如果我删除对EnableCollectionSynchronization的调用,数据被加载,但ICollectionView和它的SourceCollection将只有1项。

EF LoadAsync后刷新视图

您似乎正在使用MVVM。这是我在我的应用程序中做的。

interface ISomeDataService{
    void GetModelItems(Action<Model[], Exception> callback);
}

class SomeDataServiceImpl:ISomeDataService{
    void GetModelItems(Action<Model[], Exception> callback){
        Task.Factory.StartNew(()=>{
            //get model items from WCF for example than call the callback
            //expected to take a while. 
            //you can also directly access DbContext from here
            //if you like
            callback(res, null);
        });
    }
}

你现在需要在你的VM中使用这个实现。你可以这样做:

class MyDemoVM{
    private Model[] items;
    private readonly ISomeDataService service;
    private readonly IDispatcherService dispService;
    public MyDemoVM(){
        service=new SomeDataServiceImpl();
        dispService=new DispatcherService();
        //i use ctor injection here in order to break the dependency on SomeDataServiceImpl and on DispatcherService.
        //DispatcherService delegates to the App dispatcher in order to run code
        //on the UI thread.
    }
    public Model[] Items{
        get{
            if(items==null)GetItems();
            return items;
        }
        set{
            if(items==value)return;
            items=value;
            NotifyChanged("Items");
        }
    }
    private void GetItems(){
        service.GetModelItems((res,ex)=>{
            //this is on a different thread so you need to synchronize
            dispService.Dispatch(()=>{
                Items=res;
            });
        });
    }
}

这段代码使用延迟加载。当UI控件读取Items属性时,数据将被下载。此代码将在下载所有数据后仅刷新一次集合。请注意,这个例子适用于。net 4。如果你使用的是。net 4.5,你可以使用async/await关键字来提高服务的可读性。概念是一样的