Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I'd approach a solution to this.</p> <p>Your ViewModel implements INotifyPropertyChanged right? There's no need to dispatch the Events. Just raise them "bare" in the Model, then dispatch the RaisePropertyChanged in the ViewModel.</p> <p>And yes, you should have some sort of singleton model/database in your code. After all, what is a SQL Database if not some gigantic singleton? Since we don't have a database in WP7, don't be shy creating a singleton object. I have one called "Database" :)</p> <p>I've just tried threading my dataloads in there, and realise that in fact the best approach is simply implementing INotifyPropertyChanged right down at the model level. <a href="http://blog.alner.net/archive/2010/02/26/should-the-domain-model-implement-inotifypropertychanged.aspx" rel="noreferrer">There's no shame in this</a>.</p> <p>So given that, here's what I'm doing in the singleton Database object to load and return my Tours "table" (note the thread.sleep to make it take a visible amount of time to load, normally its sub 100ms). Database class now implements INotifyPropertyChanged, and raises events when loading is completed:</p> <pre><code>public ObservableCollection&lt;Tour&gt; Tours { get { if ( _tours == null ) { _tours = new ObservableCollection&lt;Tour&gt;(); ThreadPool.QueueUserWorkItem(LoadTours); } return _tours; } } private void LoadTours(object o) { var start = DateTime.Now; //simlate lots of work Thread.Sleep(5000); _tours = IsoStore.Deserialize&lt;ObservableCollection&lt;Tour&gt;&gt;( ToursFilename ) ?? new ObservableCollection&lt;Tour&gt;(); Debug.WriteLine( "Deserialize time: " + DateTime.Now.Subtract( start ).ToString() ); RaisePropertyChanged("Tours"); } </code></pre> <p>You follow? I'm deserializing the Tour list on a background thread, then raising a propertychanged event.</p> <p>Now in the ViewModel, I want a list of TourViewModels to bind to, which I select with a linq query once I see that the Tours table has changed. It's probably a bit cheap to listen for the Database event in the ViewModel - it might be "nicer" to encapsulate that in the model, but let's not make work we we don't need to eh?</p> <p>Hook the Database event in the Viewmodel's constructor:</p> <pre><code>public TourViewModel() { Database.Instance.PropertyChanged += DatabasePropertyChanged; } </code></pre> <p>Listen for the appropriate table change (we love magic strings! ;-) ):</p> <pre><code>private void DatabasePropertyChanged(object sender, PropertyChangedEventArgs e) { if(e.PropertyName == "Tours") { LoadTourList(); } } </code></pre> <p>Select the records I want from the table, then tell the view there is new data:</p> <pre><code>public void LoadTourList() { AllTours = ( from t in Database.Instance.Tours select new TourViewModel( t ) ).ToList(); RaisePropertyChanged( "AllTours" ); } </code></pre> <p>And lastly, in your ViewModelBase, it's best to check if your RaisePropertyChanged needs dispatching. My "SafeDispatch" method is pretty much the same as the one from MVVMlight:</p> <pre><code>private void RaisePropertyChanged(string property) { if ( PropertyChanged != null ) { UiHelper.SafeDispatch(() =&gt; PropertyChanged(this, new PropertyChangedEventArgs(property))); } } </code></pre> <p>This works perfectly in my code, and I think is fairly tidy?</p> <p>Lastly, extra for experts: in WP7, it might be good to add a ProgressBar with IsIndeterminate=True to your page - this will display the "dotted" progress bar. Then what you can do is when the ViewModel first loads you could set a "ProgressBarVisible" property to Visible (and raise the associated PropertyChanged event). Bind the ProgressBar's visibility to this ViewModel property. When the Database PropertyChanged event fires, set the visibility to Collapsed to make the progressbar go away.</p> <p>This way the user will see the "IsIndeterminate" progress bar at the top of their screen while the deserialization is running. Nice!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload