Пример #1
0
        public static T Clone <T>(this T viewModel, bool includeCollections = true) where T : ViewModel
        {
            T clone = MVVMKernelExtensions.CreateViewModel <T>();

            foreach (var prop in viewModel.Properties)
            {
                var sourceP = prop.Property;
                var cloneP  = clone[sourceP.PropertyName].Property;

                if (!prop.IsCollectionProperty && !prop.IsComputed && !(prop.Property is StateMachine)) //regular property
                {
                    cloneP.ObjectValue = sourceP.ObjectValue;
                }
                else if (prop.IsComputed || prop.Property is StateMachine) //computed stuff
                {
                    //Those will be assigned automatically
                }
                else if (prop.IsCollectionProperty && includeCollections) //collections
                {
                    var sourceList = sourceP as IList;
                    var cloneList  = cloneP as IList;

                    for (int i = 0; i < sourceList.Count; i++)
                    {
                        cloneList.Add(sourceList[i]);
                    }
                }
            }



            return(clone);
        }
Пример #2
0
        object ITypeResolver.CreateInstance(string name, string identifier)
        {
            var type = ((ITypeResolver)this).GetType(name);

#if NETFX_CORE
            var isViewModel = type.GetTypeInfo().IsSubclassOf(typeof(ViewModel));
#else
            var isViewModel = typeof(ViewModel).IsAssignableFrom(type);
#endif

            if (isViewModel)
            {
                var contextViewModel = Container.Resolve(type, identifier);
                if (contextViewModel != null)
                {
                    return(contextViewModel);
                }
                return(MVVMKernelExtensions.CreateViewModel(type, identifier));
            }

            return(null);
        }