public void Add(string key, IPreLoadable preLoadable) { this._queue.Add(preLoadable, key); preLoadable.OnPreLoaded += new EventHandler<PreLoadingItemCompleteEventArgs>(this.PreLoadingItemCompleted); preLoadable.OnPreLoadingProgress += new EventHandler<PreLoadingProgressEventArgs>(this.PreLoadableOnPreLoadingProgress); preLoadable.OnPreLoadFailed += new EventHandler(this.PreLoadableFailed); }
/// <summary> /// Sets the view as a loading one and execute asynchronously the PreLoad method of IPreLoadable. /// Once loaded, it replaces the loading view by the freshly loaded ViewModel /// </summary> /// <param name="vm"></param> private async void AsyncLoadViewModel(IPreLoadable vm) { vm.IsPreLoadNeeded = false; // This ViewModel doesn't need loading anymore nowLoadingViewModel = currentViewModel; // Set the currently loading ViewModel currentViewModel = loadingViewModel; // Display the loading view vm.IsCurrentlyLoading = true; await Task.Factory.StartNew(vm.PreLoad); // Launch the preloading in another task vm.IsCurrentlyLoading = false; // Check if the view is a ILoadingViewModel and if it's currently in a loading state if(nowLoadingViewModel != null && !(nowLoadingViewModel as IPreLoadable).IsCurrentlyLoading) { currentViewModel = nowLoadingViewModel; // Replace the loading by the freshly loaded ViewModel nowLoadingViewModel = null; RaiseViewModelDisplayed(currentViewModel); RaisePropertyChanged("CurrentViewModel"); } }