c# - Refresh view after EF LoadAsync -
Considering the large set of data to be loaded, I want the UI to be responsive when loading data. There is currently only one work code which refreshes continuously URI which is not desired. How is the data loaded in the non-UI thread and found on the last update?
Private static object sync_lock = new object (); Private zero load () {MyEntities DB = new MyEntities (); TestEntityViewModel testEntityViewModel = (TestEntityViewModel) FindResource ("testEntityViewModel"); TestEntityViewModel.Source = DB TestEntities.Local; // Source is viewable select & lt; TestEntity & gt; Binding operation Enabled compilation synchronization (testEntityViewModel.Source, sync_lock); Db.TestEntities.LoadAsync (). Continue (New Action & Lt; Tasks & gt; (t) = & gt; {this.Dispatcher.Invoke (New Action ((=) (see; MoveCurrentToFirst; Commandmanager .invalidateRequerySuggested ();}) );})); }
Note: If I remove the call at EnableCollectionSynchronization
, the data will load but ICollectionView
and its SourceCollection < / Code> will have only 1 item.
You seem to be using MVVM. In this way I do in my app.
Interface ISOMDTD service {zero gatemodel items (action & lt; model [], exception & gt; callback); } Class SomeDataServiceImpl: ISomeDataService {Zero GetModelItems (Action & lt; Model [], Exceptions & gt; Callback) {Task. Factor Get the Model Item from Start (New) (=) => WCF callback call / one time, for example. // You can access DBCNight directly from here / if you callback (ridge, tap);}) Want; }}
You need to use this implementation in your VM. You can do it like the following.
class MyDemoVM {Personal Model [] items; Private Redoni ISomeDataService Service; Private Readonly IDispatcher service service service; Public MyDemoVM () {Service = New SomeDataServiceImpl (); Disp service = new dispatcher service (); // I use CTOr injection here to break the dependency on someDataServiceImpl and DispatcherService. I // Dispatch for Dispatcher service delivery to run // code on ii thread} Public Model [] Item {Get (if (item == Zero) GetItems return; Set {if (item == value) return; item = value; NotifyChanged ("item");}} Private Zero GetItems () {service.GetModelItems ((res, Ex) = & gt; {// This is on a separate thread so that you can synchronize the Disp service Dispatch () => (item = escape);};})}}
This code is loading lazy. Items are itemized by UI control. After downloading, it will be stored only once, after downloading all the data. Please note that this example is good for .NET 4. If you are using .NET 4.5, Can make use of async / wait keywords to make more readable The concept is however the same.
Comments
Post a Comment