private static object InstantiateConstructorArgument(IInstanceBuilderContext context, ViewNode viewNode, IParameter constructorArgument, IProperty constructorArgumentProperty, DocumentNode constructorArgumentNode)
        {
            object defaultValue;

            if (constructorArgumentProperty != null)
            {
                return(InstanceBuilderOperations.UpdatePropertyWithoutApply(context, viewNode, constructorArgumentProperty, constructorArgumentNode).Instance);
            }
            if (constructorArgumentNode != null)
            {
                ViewNode viewNode1 = InstanceBuilderOperations.UpdateChildWithoutApply(context, viewNode, viewNode.Children.Count, DocumentNodeChangeAction.Add, constructorArgumentNode);
                return(viewNode1.Instance);
            }
            IDocumentContext documentContext = viewNode.DocumentNode.Context;
            Type             targetType      = viewNode.TargetType;
            Type             runtimeType     = constructorArgument.ParameterType.RuntimeType;
            ReferenceStep    referenceStep   = constructorArgumentProperty as ReferenceStep;

            if (referenceStep == null || !referenceStep.HasDefaultValue(targetType))
            {
                bool flag = documentContext.TypeResolver.InTargetAssembly(constructorArgument.ParameterType);
                defaultValue = InstanceBuilderOperations.InstantiateType(runtimeType, flag);
            }
            else
            {
                defaultValue = referenceStep.GetDefaultValue(targetType);
            }
            if (context.IsSerializationScope)
            {
                defaultValue = documentContext.CreateNode(runtimeType, defaultValue);
            }
            return(defaultValue);
        }
示例#2
0
        public bool Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
        {
            DocumentNode          documentNode          = viewNode.DocumentNode;
            DocumentCompositeNode documentCompositeNode = documentNode as DocumentCompositeNode;
            bool flag = false;

            if (!PlatformTypes.FrameworkElement.IsAssignableFrom(viewNode.Type))
            {
                if (documentCompositeNode != null && documentCompositeNode.SupportsChildren && documentCompositeNode.Children.Count > 0)
                {
                    IList listAdapter = InstanceBuilderOperations.GetListAdapter(viewNode.Instance);
                    if (listAdapter != null)
                    {
                        listAdapter.Clear();
                    }
                }
                flag = true;
                viewNode.Instance = null;
            }
            else
            {
                IPlatformMetadata platformMetadata = documentNode.TypeResolver.PlatformMetadata;
                if (!context.IsSerializationScope)
                {
                    try
                    {
                        ViewNode            viewNode1           = viewNode;
                        FallbackControlType fallbackControlType = default(FallbackControlType);
                        viewNode1.Instance = (fallbackControlType == null ? Activator.CreateInstance <FallbackControlType>() : default(FallbackControlType));
                    }
                    catch (Exception exception)
                    {
                        viewNode.Instance = null;
                        flag = true;
                    }
                }
                else
                {
                    DocumentCompositeNode documentCompositeNode1 = context.DocumentContext.CreateNode(typeof(FallbackControlType));
                    ViewNodeId            id = context.SerializationContext.GetId(viewNode);
                    documentCompositeNode1.Properties[DesignTimeProperties.ViewNodeIdProperty] = context.DocumentContext.CreateNode(typeof(string), ViewNodeManager.ViewNodeIdConverter.ConvertToInvariantString(id));
                    viewNode.Instance = documentCompositeNode1;
                }
                if (viewNode.Instance != null && documentCompositeNode != null)
                {
                    if (documentCompositeNode != null)
                    {
                        DocumentNode exceptionSource = context.ExceptionDictionary.GetExceptionSource(viewNode);
                        Type         runtimeType     = documentNode.TypeResolver.ResolveType(PlatformTypes.FrameworkElement).RuntimeType;
                        foreach (IProperty property in context.GetProperties(viewNode))
                        {
                            try
                            {
                                ViewNode viewNode2 = InstanceBuilderOperations.UpdatePropertyWithoutApply(context, viewNode, property, documentCompositeNode.Properties[property]);
                                if (viewNode2 != null && viewNode2.Instance != null && viewNode2.DocumentNode != exceptionSource)
                                {
                                    Type          type          = (context.IsSerializationScope ? ((DocumentNode)viewNode2.Instance).TargetType : viewNode2.Instance.GetType());
                                    ReferenceStep referenceStep = property as ReferenceStep;
                                    if (referenceStep != null && referenceStep.TargetType.IsAssignableFrom(runtimeType) && PlatformTypeHelper.GetPropertyType(referenceStep).IsAssignableFrom(type))
                                    {
                                        InstanceBuilderOperations.SetValue(viewNode.Instance, referenceStep, viewNode2.Instance);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    IProperty property1 = platformMetadata.ResolveProperty(KnownProperties.FrameworkElementMinWidthProperty);
                    InstanceBuilderOperations.SetValue(viewNode.Instance, property1, FallbackInstanceBuilder <FallbackControlType> .MinSize(context));
                    IProperty property2 = platformMetadata.ResolveProperty(KnownProperties.FrameworkElementMinHeightProperty);
                    InstanceBuilderOperations.SetValue(viewNode.Instance, property2, FallbackInstanceBuilder <FallbackControlType> .MinSize(context));
                }
            }
            if (flag)
            {
                viewNode.Instance = ClrObjectInstanceBuilder.InvalidObjectSentinel;
            }
            viewNode.InstanceState = InstanceState.Valid;
            return(true);
        }
示例#3
0
 public override void UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode)
 {
     InstanceBuilderOperations.UpdatePropertyWithoutApply(context, viewNode, propertyKey, valueNode);
 }
        public override void UpdateProperty(IInstanceBuilderContext context, ViewNode viewNode, IProperty propertyKey, DocumentNode valueNode)
        {
            string        instance;
            string        str;
            IPropertyId   shadowProperty;
            ReferenceStep referenceStep = propertyKey as ReferenceStep;
            ViewNode      item          = viewNode.Properties[propertyKey];

            if (item != null && DocumentNodeUtilities.IsBinding(item.DocumentNode) && referenceStep != null)
            {
                ReferenceStep referenceStep1 = referenceStep;
                if (context.UseShadowProperties)
                {
                    shadowProperty = DesignTimeProperties.GetShadowProperty(propertyKey, viewNode.DocumentNode.Type);
                }
                else
                {
                    shadowProperty = null;
                }
                IPropertyId propertyId = shadowProperty;
                if (propertyId != null && DesignTimeProperties.UseShadowPropertyForInstanceBuilding(context.DocumentContext.TypeResolver, propertyId))
                {
                    referenceStep1 = propertyId as ReferenceStep;
                }
                if (referenceStep1 != null)
                {
                    IInstantiatedElementViewNode instantiatedElementViewNode = viewNode as IInstantiatedElementViewNode;
                    if (instantiatedElementViewNode == null)
                    {
                        referenceStep1.ClearValue(viewNode.Instance);
                    }
                    else
                    {
                        foreach (object instantiatedElement in instantiatedElementViewNode.InstantiatedElements)
                        {
                            referenceStep1.ClearValue(instantiatedElement);
                        }
                    }
                }
            }
            if (propertyKey.DeclaringType.Metadata.IsNameProperty(propertyKey) && InstanceBuilderOperations.GetIsInlinedResourceWithoutNamescope(viewNode))
            {
                InstanceBuilderOperations.UpdatePropertyWithoutApply(context, viewNode, propertyKey, valueNode);
                return;
            }
            INameScope nameScope = context.NameScope;
            bool       flag      = (nameScope == null ? false : propertyKey.DeclaringType.Metadata.IsNameProperty(propertyKey));

            if (flag)
            {
                ViewNode item1 = viewNode.Properties[propertyKey];
                if (item1 != null)
                {
                    str = item1.Instance as string;
                }
                else
                {
                    str = null;
                }
                string str1 = str;
                if (!string.IsNullOrEmpty(str1) && nameScope.FindName(str1) != null)
                {
                    context.NameScope.UnregisterName(str1);
                }
            }
            if ((!context.IsSerializationScope || !(propertyKey is Event)) && (context.IsSerializationScope || !DesignTimeProperties.IsDocumentOnlyDesignTimeProperty(propertyKey)))
            {
                base.UpdateProperty(context, viewNode, propertyKey, valueNode);
            }
            else
            {
                InstanceBuilderOperations.UpdatePropertyWithoutApply(context, viewNode, propertyKey, valueNode);
            }
            if (flag)
            {
                ViewNode viewNode1 = viewNode.Properties[propertyKey];
                if (viewNode1 != null)
                {
                    instance = viewNode1.Instance as string;
                }
                else
                {
                    instance = null;
                }
                string str2 = instance;
                if (!string.IsNullOrEmpty(str2))
                {
                    try
                    {
                        if (!str2.StartsWith("~", StringComparison.Ordinal) && !str2.Contains("."))
                        {
                            if (nameScope.FindName(str2) != null)
                            {
                                nameScope.UnregisterName(str2);
                            }
                            nameScope.RegisterName(str2, viewNode.Instance);
                        }
                    }
                    catch (ArgumentException argumentException1)
                    {
                        ArgumentException argumentException = argumentException1;
                        ViewNodeManager   viewNodeManager   = viewNode1.ViewNodeManager;
                        CultureInfo       currentCulture    = CultureInfo.CurrentCulture;
                        string            instanceBuilderUnableToRegisterName = ExceptionStringTable.InstanceBuilderUnableToRegisterName;
                        object[]          objArray = new object[] { str2 };
                        viewNodeManager.OnException(viewNode1, new InstanceBuilderException(string.Format(currentCulture, instanceBuilderUnableToRegisterName, objArray), argumentException, viewNode1.DocumentNode, viewNode), false);
                    }
                }
            }
        }