private static void OnViewModelRestored(IViewModelProvider sender, ViewModelRestoredEventArgs args) { var mainViewModel = args.ViewModel as MainViewModel; if (mainViewModel != null) { var context = args.State.GetData(SelectedItemState); if (context != null) { mainViewModel.SelectedItem = sender.RestoreViewModel(context, DataContext.Empty, false); } } }
public IViewModel RestoreViewModel(IDataContext viewModelState, IDataContext dataContext, bool throwOnError) { try { dataContext = dataContext.ToNonReadOnly(); if (viewModelState == null) { viewModelState = DataContext.Empty; } else { dataContext.Merge(viewModelState); } IViewModel viewModel; if (!dataContext.GetData(InitializationConstants.IgnoreViewModelCache)) { Guid id; if (viewModelState.TryGetData(IdViewModelConstant, out id)) { viewModel = GetOrAddRestoredViewModel(id).GetViewModel(); if (viewModel != null) { return(viewModel); } } } RestoredViewModel restoredParentViewModel = null; IViewModel parentViewModel = null; Guid idParent; if (viewModelState.TryGetData(IdParentViewModelConstant, out idParent)) { restoredParentViewModel = GetOrAddRestoredViewModel(idParent); parentViewModel = restoredParentViewModel.GetViewModel(); if (parentViewModel != null) { dataContext.AddOrUpdate(InitializationConstants.ParentViewModel, parentViewModel); } } var restoring = Restoring; if (restoring != null) { var args = new ViewModelRestoringEventArgs { Context = dataContext, ViewModelState = viewModelState }; restoring(this, args); dataContext = args.Context ?? DataContext.Empty; } viewModel = RestoreViewModelInternal(viewModelState, dataContext); if (viewModel != null) { if (restoredParentViewModel != null && parentViewModel == null) { restoredParentViewModel.AddChildViewModel(viewModel); } OnViewModelRestored(viewModel, viewModelState, dataContext); var restored = Restored; if (restored != null) { var args = new ViewModelRestoredEventArgs(viewModel) { Context = dataContext, ViewModelState = viewModelState }; restored(this, args); } Tracer.TraceViewModel(AuditAction.Restored, viewModel); if (ReferenceEquals(viewModelState, DataContext.Empty)) { Tracer.Warn("The view model '{0}' was restored without state.", viewModel); } return(viewModel); } if (throwOnError) { throw ExceptionManager.ViewModelCannotBeRestored(); } } catch (Exception e) { if (throwOnError) { throw; } Tracer.Warn(e.Flatten(true)); } return(null); }
public void ProviderShouldRestorePreserveViewModelState() { bool restoringCalled = false; bool restoredCalled = false; bool preservingCalled = false; bool preservedCalled = false; var preserveCtx = new DataContext(); var restoreCtx = new DataContext(); DataConstant <string> key = "key"; const string value = "value"; var loadViewModel = new ViewModel(); IocContainer.GetFunc = (type, s, arg3) => { typeof(ViewModel).ShouldEqual(type); return(loadViewModel); }; var viewModel = new ViewModel(); viewModel.Settings.State.Add(key, value); ViewModelProvider provider = GetViewModelProvider(IocContainer); provider.Preserving += (sender, args) => { sender.ShouldEqual(provider); args.Context.ShouldEqual(preserveCtx); args.ViewModel.ShouldEqual(viewModel); preservingCalled = true; }; ViewModelPreservedEventArgs preservedEventArgs = null; provider.Preserved += (sender, args) => { sender.ShouldEqual(provider); args.Context.ShouldEqual(preserveCtx); args.ViewModel.ShouldEqual(viewModel); preservedEventArgs = args; preservedCalled = true; }; var state = provider.PreserveViewModel(viewModel, preserveCtx); state.ShouldEqual(preservedEventArgs.State); provider.Restoring += (sender, args) => { sender.ShouldEqual(provider); args.State.ShouldEqual(state); restoringCalled = true; }; ViewModelRestoredEventArgs restoredEventArgs = null; provider.Restored += (sender, args) => { sender.ShouldEqual(provider); args.State.ShouldEqual(state); restoredEventArgs = args; restoredCalled = true; }; var restoreViewModel = provider.RestoreViewModel(state, restoreCtx, true); restoreViewModel.ShouldEqual(restoredEventArgs.ViewModel); restoreViewModel.IocContainer.ShouldEqual(IocContainer); restoreViewModel.ShouldEqual(loadViewModel); restoreViewModel.Settings.State.GetData(key).ShouldEqual(value); restoringCalled.ShouldBeTrue(); restoredCalled.ShouldBeTrue(); preservingCalled.ShouldBeTrue(); preservedCalled.ShouldBeTrue(); }