// -- FILE ------------------------------------------------------------------ // name : DataItemModel.cs // project : Itenso Community // created : Jani Giannoudis - 2012.05.05 // language : c# // environment: .NET 4.0 // copyright : (c) 2004-2012 by Itenso GmbH, Switzerland // -------------------------------------------------------------------------- using System; using Itenso.Community.XamlPatterns.Windows; namespace Itenso.Community.XamlPatterns.Presentation.Dependency { // ------------------------------------------------------------------------ public abstract class DataItemModel : ItemModel, IDataItemModel { // ---------------------------------------------------------------------- public event EventHandler Loading; // ---------------------------------------------------------------------- public event EventHandler Loaded; // ---------------------------------------------------------------------- public bool IsLoading { get { return isLoading; } private set { if ( value != isLoading ) { isLoading = value; NotifyPropertyChanged( "IsLoading" ); } } } // IsLoading // ---------------------------------------------------------------------- protected virtual bool LoadAsync { get { return false; } } // LoadAsync // ---------------------------------------------------------------------- public void Load() { if ( IsLoading ) { throw new InvalidOperationException(); } IsLoading = true; OnLoading(); if ( LoadAsync ) { // ReSharper disable RedundantDelegateCreation AsyncDispatcher.Current.BeginInvoke( new Action( InvokeLoad ) ); // ReSharper restore RedundantDelegateCreation } else { InvokeLoad(); } } // Load // ---------------------------------------------------------------------- protected virtual void OnLoading() { EventHandler loading = Loading; if ( loading != null ) { loading( this, EventArgs.Empty ); } } // OnLoading // ---------------------------------------------------------------------- protected virtual bool DoLoad() { return true; } // DoLoad // ---------------------------------------------------------------------- protected void LoadFinished() { if ( !IsLoading ) { throw new InvalidOperationException(); } OnLoaded(); IsLoading = false; } // LoadFinished // ---------------------------------------------------------------------- protected virtual void OnLoaded() { EventHandler loaded = Loaded; if ( loaded != null ) { loaded( this, EventArgs.Empty ); } } // OnLoaded // ---------------------------------------------------------------------- private void InvokeLoad() { if ( DoLoad() ) { LoadFinished(); } } // InvokeLoad // ---------------------------------------------------------------------- // members private bool isLoading; } // class HierarchicalItemModel } // namespace Itenso.Community.XamlPatterns.Presentation.Dependency // -- EOF -------------------------------------------------------------------
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)