public override void ModifyValue(IInstanceBuilderContext context, ViewNode target, object onlyThisInstance, IProperty propertyKey, object value, PropertyModification modification) { IProperty shadowProperty; if (!context.IsSerializationScope) { IInstantiatedElementViewNode instantiatedElementViewNode = target as IInstantiatedElementViewNode; if (instantiatedElementViewNode != null && onlyThisInstance == null && instantiatedElementViewNode.InstantiatedElements.First != null) { if (context.UseShadowProperties) { shadowProperty = DesignTimeProperties.GetShadowProperty(propertyKey, target.DocumentNode.Type); } else { shadowProperty = null; } IProperty property = shadowProperty; if (property != null && DesignTimeProperties.UseShadowPropertyForInstanceBuilding(target.TypeResolver, property)) { propertyKey = property; } ReferenceStep referenceStep = propertyKey as ReferenceStep; if (referenceStep != null) { foreach (object instantiatedElement in instantiatedElementViewNode.InstantiatedElements) { if (modification != PropertyModification.Set) { referenceStep.ClearValue(instantiatedElement); } else { InstanceBuilderOperations.SetValue(instantiatedElement, referenceStep, value); } } return; } } } if (propertyKey.DeclaringType.Metadata.IsNameProperty(propertyKey)) { string valueAsString = null; if (context.IsSerializationScope) { DocumentPrimitiveNode documentPrimitiveNode = value as DocumentPrimitiveNode; if (documentPrimitiveNode != null) { valueAsString = DocumentPrimitiveNode.GetValueAsString(documentPrimitiveNode); } } else { valueAsString = value as string; if (string.IsNullOrEmpty(valueAsString) && (ProjectNeutralTypes.VisualStateGroup.IsAssignableFrom(target.Type) || ProjectNeutralTypes.VisualState.IsAssignableFrom(target.Type))) { valueAsString = ((DocumentCompositeNode)target.DocumentNode).GetValueAsString(target.DocumentNode.NameProperty); } } if (valueAsString != null && string.IsNullOrEmpty(valueAsString)) { CultureInfo currentCulture = CultureInfo.CurrentCulture; string instanceBuilderNamePropertyNotValid = ExceptionStringTable.InstanceBuilderNamePropertyNotValid; object[] objArray = new object[] { valueAsString }; throw new InstanceBuilderException(string.Format(currentCulture, instanceBuilderNamePropertyNotValid, objArray), target.DocumentNode); } } base.ModifyValue(context, target, onlyThisInstance, propertyKey, value, modification); }
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); }
public static bool SetValue(object target, IProperty propertyKey, object value) { if (!InstanceBuilderOperations.IsSupported((IMember)propertyKey)) { return(false); } if (DesignTimeProperties.InlineXmlProperty.Equals((object)propertyKey)) { IXmlSerializable serializable = target as IXmlSerializable; if (serializable == null) { return(false); } InstanceBuilderOperations.SetXmlContent(serializable, (string)value); return(true); } DocumentCompositeNode documentCompositeNode = target as DocumentCompositeNode; if (documentCompositeNode != null) { IProperty property = (IProperty)propertyKey.Clone(documentCompositeNode.TypeResolver); documentCompositeNode.Properties[(IPropertyId)property] = (DocumentNode)value; return(true); } ReferenceStep referenceStep = propertyKey as ReferenceStep; if (referenceStep != null) { object obj = value; MarkupExtension markupExtension; if ((markupExtension = obj as MarkupExtension) != null) { if (markupExtension is TemplateBindingExtension) { throw new InstanceBuilderException(ExceptionStringTable.InvalidTemplateBindingInstanceBuilderException); } DependencyPropertyReferenceStep propertyReferenceStep1; object property; if ((propertyReferenceStep1 = referenceStep as DependencyPropertyReferenceStep) != null) { property = propertyReferenceStep1.DependencyProperty; } else { ClrPropertyReferenceStep propertyReferenceStep2; if ((propertyReferenceStep2 = referenceStep as ClrPropertyReferenceStep) == null) { return(false); } property = (object)propertyReferenceStep2.PropertyInfo; } DynamicResourceExtension resourceExtension = markupExtension as DynamicResourceExtension; FrameworkElement frameworkElement = target as FrameworkElement; FrameworkContentElement frameworkContentElement = target as FrameworkContentElement; if (resourceExtension != null && resourceExtension.ResourceKey != null && propertyReferenceStep1 != null && (frameworkElement != null || frameworkContentElement != null)) { if (frameworkElement != null) { propertyReferenceStep1.SetResourceReference((object)frameworkElement, resourceExtension.ResourceKey); } else if (frameworkContentElement != null) { propertyReferenceStep1.SetResourceReference((object)frameworkContentElement, resourceExtension.ResourceKey); } } else { if (Microsoft.Expression.DesignModel.Metadata.KnownProperties.SetterValueProperty.Equals((object)referenceStep)) { referenceStep.SetValue(target, obj); return(true); } bool flag = false; try { obj = markupExtension.ProvideValue((IServiceProvider) new InstanceBuilderOperations.InstanceBuilderServiceProvider(target, property, (IMetadataResolver)referenceStep.DeclaringType.PlatformMetadata)); flag = true; } catch (InvalidOperationException ex) { } if (flag) { if (!(obj is MarkupExtension)) { return(InstanceBuilderOperations.SetValue(target, (IProperty)referenceStep, obj)); } referenceStep.SetValue(target, obj); return(true); } } } else { //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx //if (obj is Expression) //{ // referenceStep.SetValue(target, obj); // return true; //} if (obj != null) { bool flag = false; DependencyPropertyReferenceStep propertyReferenceStep = referenceStep as DependencyPropertyReferenceStep; if (propertyReferenceStep != null) { ITypeId type = (ITypeId)propertyReferenceStep.PlatformTypes.GetType(obj.GetType()); if (PlatformTypes.Binding.IsAssignableFrom(type)) { flag = true; } } if (flag) { propertyReferenceStep.SetBinding(target, value); return(true); } if (!PlatformTypeHelper.GetPropertyType((IProperty)referenceStep).IsInstanceOfType(obj)) { return(false); } } referenceStep.SetValue(target, obj); return(true); } } return(false); }