Beispiel #1
0
 private static CachedViewModel GetOrAddCachedViewModel(Guid id)
 {
     lock (CachedViewModels)
     {
         CachedViewModel value;
         if (!CachedViewModels.TryGetValue(id, out value))
         {
             value = new CachedViewModel();
             CachedViewModels[id] = value;
         }
         return(value);
     }
 }
Beispiel #2
0
        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(ViewModelConstants.Id, out id))
                    {
                        viewModel = GetOrAddCachedViewModel(id).GetViewModel();
                        if (viewModel != null)
                        {
                            return(viewModel);
                        }
                    }
                }

                CachedViewModel restoredParentViewModel = null;
                IViewModel      parentViewModel         = null;
                Guid            idParent;
                if (viewModelState.TryGetData(ViewModelConstants.IdParent, out idParent))
                {
                    restoredParentViewModel = GetOrAddCachedViewModel(idParent);
                    parentViewModel         = restoredParentViewModel.GetViewModel();
                    if (parentViewModel != null)
                    {
                        dataContext.AddOrUpdate(InitializationConstants.ParentViewModel, parentViewModel);
                    }
                }

                var restoring = Restoring;
                if (restoring != null)
                {
                    var args = new ViewModelRestoringEventArgs {
                        Context = dataContext, State = 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,
                            State   = viewModelState
                        };
                        restored(this, args);
                    }
                    Tracer.TraceViewModel(ViewModelLifecycleType.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) when(!throwOnError)
            {
                Tracer.Warn(e.Flatten(true));
            }
            return(null);
        }
 private static CachedViewModel GetOrAddCachedViewModel(Guid id)
 {
     lock (CachedViewModels)
     {
         CachedViewModel value;
         if (!CachedViewModels.TryGetValue(id, out value))
         {
             value = new CachedViewModel();
             CachedViewModels[id] = value;
         }
         return value;
     }
 }