public static DocumentCompositeNode CreateDataTemplateResource(SceneNode targetElement, IPropertyId templateProperty, string resourceNameBase, IList <DataSchemaNodePath> entries, DataViewCategory category, ITypeId dataTemplateType) { SceneViewModel viewModel = targetElement.ViewModel; IPlatform platform = targetElement.Platform; IDocumentContext documentContext = viewModel.Document.DocumentContext; DocumentCompositeNode documentCompositeNode = DataViewFactory.GenerateDataView(platform, documentContext, entries, category); if (documentCompositeNode == null) { return((DocumentCompositeNode)null); } DocumentCompositeNode node = documentContext.CreateNode(dataTemplateType); node.Properties[(IPropertyId)node.Type.Metadata.DefaultContentProperty] = (DocumentNode)documentCompositeNode; CreateResourceModel createResourceModel = new CreateResourceModel(viewModel, viewModel.DesignerContext.ResourceManager, node.Type.RuntimeType, (Type)null, (string)null, (SceneElement)null, (SceneNode)null, CreateResourceModel.ContextFlags.None); createResourceModel.KeyString = resourceNameBase; bool useStaticResource = !JoltHelper.TypeSupported((ITypeResolver)viewModel.ProjectContext, PlatformTypes.DynamicResource); if (!useStaticResource && !PlatformTypes.FrameworkElement.IsAssignableFrom((ITypeId)targetElement.Type)) { useStaticResource = true; } int index = useStaticResource ? createResourceModel.IndexInResourceSite(targetElement.DocumentNode) : -1; DocumentCompositeNode resource = createResourceModel.CreateResource((DocumentNode)node, (IPropertyId)null, index); if (templateProperty != null) { DocumentNode resourceReference = createResourceModel.CreateResourceReference(documentContext, resource, useStaticResource); using (viewModel.AnimationEditor.DeferKeyFraming()) targetElement.SetValue(templateProperty, (object)resourceReference); } return((DocumentCompositeNode)resource.Properties[DictionaryEntryNode.ValueProperty]); }
internal static void EnsureReferencedResourcesAreReachable(DocumentNode resourceNode, DocumentNode resourceExtensionNode) { if (resourceNode == null || resourceExtensionNode == null || resourceExtensionNode.DocumentRoot != resourceNode.DocumentRoot) { return; } DocumentCompositeNode dictionaryNode = resourceNode.Parent.Parent; int index1 = CreateResourceModel.IndexInResourceSite(resourceExtensionNode, dictionaryNode); if (index1 == -1) { return; } List <DocumentCompositeNode> list1 = Enumerable.ToList <DocumentCompositeNode>(Enumerable.Where <DocumentCompositeNode>((IEnumerable <DocumentCompositeNode>)ResourceHelper.FindReferencedResources(resourceNode), (Func <DocumentCompositeNode, bool>)(node => node.Parent == dictionaryNode))); if (!list1.Contains(resourceNode.Parent)) { list1.Add(resourceNode.Parent); } list1.Sort((Comparison <DocumentCompositeNode>)((lhs, rhs) => lhs.SiteChildIndex.CompareTo(rhs.SiteChildIndex))); List <DocumentCompositeNode> list2 = new List <DocumentCompositeNode>(); for (int index2 = list1.Count - 1; index2 >= 0; --index2) { if (list1[index2].SiteChildIndex > index1) { list2.Add(list1[index2]); dictionaryNode.Children.RemoveAt(list1[index2].SiteChildIndex); } } foreach (DocumentCompositeNode documentCompositeNode in list2) { dictionaryNode.Children.Insert(index1, (DocumentNode)documentCompositeNode); } }
public override void Execute() { BaseFrameworkElement selectedElement = this.SelectedElement; if (selectedElement == null) { return; } IType type1 = this.SceneViewModel.ProjectContext.ResolveType(PlatformTypes.Style); CreateResourceModel.ContextFlags contextFlags = this.SceneViewModel.ProjectContext.IsCapabilitySet(PlatformCapability.SupportsImplicitStyles) ? CreateResourceModel.ContextFlags.CanApplyAutomatically : CreateResourceModel.ContextFlags.None; if (this.SceneViewModel.ProjectContext.IsCapabilitySet(PlatformCapability.SupportsImplicitStyles)) { contextFlags = CreateResourceModel.ContextFlags.CanApplyAutomatically; } CreateResourceModel createResourceModel = new CreateResourceModel(this.SceneViewModel, this.DesignerContext.ResourceManager, type1.RuntimeType, this.DefaultContainerType.RuntimeType, PlatformTypes.Style.Name, (SceneElement)null, (SceneNode)selectedElement, contextFlags); if (this.ShowUI) { bool?nullable = this.CreateDialog(createResourceModel).ShowDialog(); if ((!nullable.GetValueOrDefault() ? 1 : (!nullable.HasValue ? true : false)) != 0) { return; } } using (this.SceneViewModel.DisableUpdateChildrenOnAddAndRemove()) { using (SceneEditTransaction editTransaction1 = this.SceneViewModel.CreateEditTransaction(this.UndoString, false)) { using (this.SceneViewModel.ForceBaseValue()) { using (this.SceneViewModel.DisableDrawIntoState()) { this.SceneViewModel.AnimationEditor.DeleteAllAnimationsInSubtree((SceneElement)selectedElement); this.SceneViewModel.ElementSelectionSet.Clear(); IDocumentContext documentContext = this.SceneViewModel.Document.DocumentContext; IProjectContext projectContext = this.SceneViewModel.Document.ProjectContext; Type type2 = (Type)null; if (createResourceModel.TargetTypeAsset != null && createResourceModel.TargetTypeAsset.EnsureTypeReferenced(this.SceneViewModel.ProjectContext as ProjectContext)) { type2 = createResourceModel.TargetTypeAsset.Type.RuntimeType; } if (type2 == (Type)null) { type2 = createResourceModel.TargetType; } IType type3 = projectContext.GetType(type2); DocumentCompositeNode documentCompositeNode1 = (DocumentCompositeNode)selectedElement.DocumentNode; this.SceneViewModel.GetLayoutDesignerForChild((SceneElement)selectedElement, true).ClearUnusedLayoutProperties(selectedElement); DocumentCompositeNode visualTreeNode = documentContext.CreateNode((ITypeId)documentCompositeNode1.Type); Dictionary <IProperty, DocumentNode> dictionary = new Dictionary <IProperty, DocumentNode>(); bool addRenderTransforms = false; foreach (IPropertyId propertyId in this.GetLayoutProperties((SceneElement)selectedElement, addRenderTransforms)) { IProperty property = this.DesignerContext.ActiveSceneViewModel.ProjectContext.ResolveProperty(propertyId); if (property != null && documentCompositeNode1.Properties.Contains(property)) { dictionary.Add(property, documentCompositeNode1.Properties[(IPropertyId)property].Clone(documentContext)); documentCompositeNode1.ClearValue((IPropertyId)property); } } foreach (KeyValuePair <IProperty, DocumentNode> keyValuePair in (IEnumerable <KeyValuePair <IProperty, DocumentNode> >)documentCompositeNode1.Properties) { IPropertyId index = (IPropertyId)keyValuePair.Key; DependencyPropertyReferenceStep propertyReferenceStep = index as DependencyPropertyReferenceStep; if ((propertyReferenceStep == null || !propertyReferenceStep.IsAttachable || propertyReferenceStep.MemberType == MemberType.DesignTimeProperty) && (!index.Equals((object)BaseFrameworkElement.WidthProperty) && !index.Equals((object)BaseFrameworkElement.HeightProperty))) { visualTreeNode.Properties[index] = keyValuePair.Value.Clone(documentContext); } } if (documentCompositeNode1.SupportsChildren) { foreach (DocumentNode documentNode in (IEnumerable <DocumentNode>)documentCompositeNode1.Children) { visualTreeNode.Children.Add(documentNode.Clone(documentContext)); } } if (!PlatformTypes.Panel.IsAssignableFrom((ITypeId)documentCompositeNode1.Type)) { GridElement gridElement = (GridElement)this.SceneViewModel.CreateSceneNode(PlatformTypes.Grid); SceneNode sceneNode = this.SceneViewModel.GetSceneNode((DocumentNode)visualTreeNode); gridElement.Children.Add(sceneNode); visualTreeNode = (DocumentCompositeNode)gridElement.DocumentNode; } StyleNode styleNode = (StyleNode)this.SceneViewModel.CreateSceneNode(PlatformTypes.Style); styleNode.StyleTargetTypeId = type3; SetterSceneNode setterSceneNode = (SetterSceneNode)this.SceneViewModel.CreateSceneNode(PlatformTypes.Setter); DependencyPropertyReferenceStep propertyReferenceStep1 = (DependencyPropertyReferenceStep)this.SceneViewModel.ProjectContext.ResolveProperty(ControlElement.TemplateProperty); setterSceneNode.Property = propertyReferenceStep1; BaseFrameworkElement frameworkElement = (BaseFrameworkElement)this.SceneViewModel.CreateSceneNode(type2); DocumentCompositeNode documentCompositeNode2 = (DocumentCompositeNode)frameworkElement.DocumentNode; this.AddPresenterIfNecessary(visualTreeNode, (SceneElement)frameworkElement); ControlTemplateElement controlTemplateElement = (ControlTemplateElement)this.SceneViewModel.CreateSceneNode(PlatformTypes.ControlTemplate); controlTemplateElement.ControlTemplateTargetTypeId = (ITypeId)type3; controlTemplateElement.DefaultInsertionPoint.Insert(this.SceneViewModel.GetSceneNode((DocumentNode)visualTreeNode)); if (PlatformTypes.Button.Equals((object)type3) && controlTemplateElement.CanEditTriggers && this.SceneViewModel.ProjectContext.IsCapabilitySet(PlatformCapability.IsWpf)) { DocumentCompositeNode node = documentContext.CreateNode(typeof(TriggerCollection)); node.Children.Add((DocumentNode)this.CreatePropertyTrigger(documentContext, ButtonProperties.IsFocusedProperty, (object)true)); node.Children.Add((DocumentNode)this.CreatePropertyTrigger(documentContext, ButtonProperties.IsDefaultedProperty, (object)true)); node.Children.Add((DocumentNode)this.CreatePropertyTrigger(documentContext, BaseFrameworkElement.IsMouseOverProperty, (object)true)); node.Children.Add((DocumentNode)this.CreatePropertyTrigger(documentContext, ButtonProperties.IsPressedProperty, (object)true)); node.Children.Add((DocumentNode)this.CreatePropertyTrigger(documentContext, BaseFrameworkElement.IsEnabledProperty, (object)false)); controlTemplateElement.SetLocalValue(ControlTemplateElement.ControlTemplateTriggersProperty, (DocumentNode)node); } setterSceneNode.SetValueAsSceneNode(SetterSceneNode.ValueProperty, (SceneNode)controlTemplateElement); styleNode.Setters.Add((SceneNode)setterSceneNode); bool useStaticResource = !JoltHelper.TypeSupported((ITypeResolver)this.SceneViewModel.ProjectContext, PlatformTypes.DynamicResource); int index1 = -1; if (useStaticResource && selectedElement.DocumentContext == createResourceModel.CurrentResourceSite.DocumentContext) { index1 = createResourceModel.IndexInResourceSite(selectedElement.DocumentNode); } IList <DocumentCompositeNode> referencedResources = Microsoft.Expression.DesignSurface.Utility.ResourceHelper.FindReferencedResources((DocumentNode)documentCompositeNode1); foreach (KeyValuePair <IProperty, DocumentNode> keyValuePair in dictionary) { documentCompositeNode2.Properties[(IPropertyId)keyValuePair.Key] = keyValuePair.Value; } IList <SceneNode> nodes = (IList <SceneNode>) new List <SceneNode>(); nodes.Add((SceneNode)frameworkElement); SceneNode parent = selectedElement.Parent; IProperty propertyForChild = parent.GetPropertyForChild((SceneNode)selectedElement); ISceneNodeCollection <SceneNode> collectionForProperty = parent.GetCollectionForProperty((IPropertyId)propertyForChild); int index2 = collectionForProperty.IndexOf((SceneNode)selectedElement); collectionForProperty[index2] = (SceneNode)frameworkElement; if (createResourceModel.SelectedResourceDictionary != null) { ResourceContainer instance = createResourceModel.SelectedResourceDictionary.Instance; if (instance != null && instance.DocumentNode == documentCompositeNode1) { createResourceModel = new CreateResourceModel(this.SceneViewModel, this.DesignerContext.ResourceManager, type1.RuntimeType, type2, PlatformTypes.Style.Name, (SceneElement)frameworkElement, (SceneNode)null, contextFlags); } } if (createResourceModel.CurrentResourceSite != null && !PlatformTypes.PlatformsCompatible(createResourceModel.CurrentResourceSite.DocumentContext.TypeResolver.PlatformMetadata, styleNode.DocumentNode.PlatformMetadata)) { editTransaction1.Cancel(); return; } DocumentCompositeNode resource = createResourceModel.CreateResource(styleNode.DocumentNode, StyleNode.TargetTypeProperty, index1); if (resource == null) { editTransaction1.Cancel(); return; } DocumentNode resourceReference = createResourceModel.CreateResourceReference(this.SceneViewModel.Document.DocumentContext, resource, useStaticResource); DefaultTypeInstantiator typeInstantiator = new DefaultTypeInstantiator(this.SceneView); if (resourceReference != null) { documentCompositeNode2.Properties[BaseFrameworkElement.StyleProperty] = resourceReference; } foreach (KeyValuePair <IProperty, DocumentNode> keyValuePair in (IEnumerable <KeyValuePair <IProperty, DocumentNode> >)documentCompositeNode1.Properties) { IPropertyId index3 = (IPropertyId)keyValuePair.Key; DependencyPropertyReferenceStep propertyReferenceStep2 = index3 as DependencyPropertyReferenceStep; if (propertyReferenceStep2 != null && propertyReferenceStep2.IsAttachable && propertyReferenceStep2.MemberType != MemberType.DesignTimeProperty) { documentCompositeNode2.Properties[index3] = keyValuePair.Value.Clone(documentContext); } } DocumentCompositeNode hostNode = createResourceModel.CurrentResourceSite.HostNode; SceneViewModel viewModel = this.SceneViewModel.GetViewModel(hostNode.DocumentRoot, false); using (SceneEditTransaction editTransaction2 = viewModel.CreateEditTransaction(this.UndoString)) { Microsoft.Expression.DesignSurface.Utility.ResourceHelper.CopyResourcesToNewResourceSite(referencedResources, viewModel, hostNode, resource, createResourceModel.IndexInResourceSite((DocumentNode)resource)); editTransaction2.Commit(); } editTransaction1.Update(); if (this.SceneView.IsValid) { typeInstantiator.ApplyAfterInsertionDefaultsToElements(nodes, (SceneNode)null); } this.SceneView.CandidateEditingContainer = frameworkElement.DocumentNodePath; editTransaction1.Update(); this.SceneViewModel.ElementSelectionSet.ExtendSelection((SceneElement)frameworkElement); this.PostProcessing((SceneNode)selectedElement, (SceneNode)frameworkElement, resource); if (frameworkElement.GetComputedValue(ControlElement.TemplateProperty) != null) { this.ActivateTemplateEditingMode((SceneElement)frameworkElement); } else { UIThreadDispatcherHelper.BeginInvoke(DispatcherPriority.ApplicationIdle, (Delegate) new Action <SceneElement>(this.ActivateTemplateEditingMode), (object)frameworkElement); } this.SceneView.CandidateEditingContainer = (DocumentNodePath)null; } editTransaction1.Commit(); } } } }
public override void Execute() { PerformanceUtility.StartPerformanceSequence(PerformanceEvent.EditStyleOrTemplate); if (this.IsEnabled) { SceneElement targetElement = this.TargetElement; IDocumentContext documentContext = this.SceneViewModel.Document.DocumentContext; Type propertyTargetType = this.SceneViewModel.Document.ProjectContext.MetadataFactory.GetMetadata(this.Type.RuntimeType).GetStylePropertyTargetType((IPropertyId)this.TargetProperty); CreateResourceModel.ContextFlags contextFlags = !PlatformTypes.ControlTemplate.IsAssignableFrom((ITypeId)this.TargetProperty.PropertyType) ? (this.SceneViewModel.ProjectContext.IsCapabilitySet(PlatformCapability.SupportsImplicitStyles) ? CreateResourceModel.ContextFlags.CanApplyAutomatically : CreateResourceModel.ContextFlags.None) : CreateResourceModel.ContextFlags.None; CreateResourceModel model = new CreateResourceModel(this.SceneViewModel, this.DesignerContext.ResourceManager, PlatformTypeHelper.GetPropertyType((IProperty)this.TargetProperty), propertyTargetType, this.TargetProperty.Name, (SceneElement)null, (SceneNode)(targetElement as BaseFrameworkElement), contextFlags); IList <DocumentCompositeNode> auxillaryResources; DocumentNode node1 = this.ProvideValue(out auxillaryResources); if (node1 != null) { IPropertyId targetTypeProperty = this.GetTargetTypeProperty((ITypeId)this.TargetProperty.PropertyType); DocumentCompositeNode documentCompositeNode = node1 as DocumentCompositeNode; if (targetTypeProperty != null && documentCompositeNode != null) { IType valueAsType = DocumentPrimitiveNode.GetValueAsType(documentCompositeNode.Properties[targetTypeProperty]); if (valueAsType != null && valueAsType.RuntimeType != (Type)null) { model.TargetType = valueAsType.RuntimeType; } } else { model.TargetType = (Type)null; } ReplaceStyleTemplateCommand.ExtraReferences references = new ReplaceStyleTemplateCommand.ExtraReferences(); this.CollectExtraReferences(node1, references); if (auxillaryResources != null) { foreach (DocumentNode node2 in (IEnumerable <DocumentCompositeNode>)auxillaryResources) { this.CollectExtraReferences(node2, references); } } if (references.UnresolvedTypes.Count > 0) { string message = string.Format((IFormatProvider)CultureInfo.CurrentCulture, StringTable.CopyStyleTemplateTypesNotInDocumentMessage, new object[2] { (object)this.TargetProperty.Name, (object)SceneView.GetUnresolvedTypesList(references.UnresolvedTypes) }); if (!this.ShowUI) { return; } this.DesignerContext.MessageDisplayService.ShowError(message); return; } if (this.ShowUI) { bool?nullable = new CreateResourceDialog(this.DesignerContext, model).ShowDialog(); if ((!nullable.GetValueOrDefault() ? 1 : (!nullable.HasValue ? true : false)) != 0) { return; } } bool flag = model.CurrentResourceSite != null; if (!flag || targetElement.DocumentNode.DocumentRoot == null) { return; } SceneViewModel viewModel = this.SceneViewModel.GetViewModel(model.CurrentResourceSite.HostNode.DocumentRoot, false); if (viewModel == null || !PlatformTypes.PlatformsCompatible(node1.PlatformMetadata, viewModel.ProjectContext.PlatformMetadata) || !this.AddReferences(viewModel.ProjectContext, references, model.KeyString)) { return; } using (SceneEditTransaction editTransaction1 = this.SceneViewModel.CreateEditTransaction(this.UndoString)) { DocumentNode documentNode1 = (DocumentNode)null; DocumentCompositeNode resource; using (SceneEditTransaction editTransaction2 = viewModel.CreateEditTransaction(this.UndoString)) { DocumentNode newResourceNode; try { newResourceNode = node1.Clone(viewModel.Document.DocumentContext); } catch { editTransaction2.Cancel(); editTransaction1.Cancel(); this.DesignerContext.MessageDisplayService.ShowError(string.Format((IFormatProvider)CultureInfo.CurrentCulture, StringTable.CommandFailedDialogMessage, new object[1] { (object)this.UndoString })); return; } newResourceNode.Name = (string)null; bool useStaticResource = !(this.TargetProperty is DependencyPropertyReferenceStep) || !JoltHelper.TypeSupported((ITypeResolver)this.SceneViewModel.ProjectContext, PlatformTypes.DynamicResource); int index = useStaticResource ? model.IndexInResourceSite(targetElement.DocumentNode) : -1; resource = model.CreateResource(newResourceNode, targetTypeProperty, index); flag = resource != null; if (flag) { documentNode1 = model.CreateResourceReference(this.SceneViewModel.Document.DocumentContext, resource, useStaticResource); flag = Microsoft.Expression.DesignSurface.Utility.ResourceHelper.CopyResourcesToNewResourceSite(auxillaryResources, viewModel, model.CurrentResourceSite.HostNode, resource, model.IndexInResourceSite((DocumentNode)resource)); } if (flag) { editTransaction2.Commit(); if (this.SceneViewModel == viewModel) { editTransaction1.Update(); this.DesignerContext.ViewUpdateManager.UpdateRelatedViews(this.SceneViewModel.Document, false); } this.DesignerContext.ViewUpdateManager.RefreshActiveViewApplicationResources(); } else { editTransaction2.Cancel(); } } if (flag && resource != null) { DocumentNode documentNode2 = resource.Properties[DictionaryEntryNode.ValueProperty]; DocumentNodePath documentNodePath; if (targetElement.IsAttached) { if (documentNode1 != null) { targetElement.SetValue(this.TargetPropertyReference, (object)documentNode1); } else { targetElement.ClearValue(this.TargetPropertyReference); } this.SceneView.CandidateEditingContainer = targetElement.DocumentNodePath; editTransaction1.Update(); this.SceneView.CandidateEditingContainer = (DocumentNodePath)null; documentNodePath = this.ProvideEditingContainer(targetElement, this.TargetPropertyReference, documentNode2); } else { documentNodePath = (DocumentNodePath)null; } if (this.SceneView.IsValid) { if (documentNodePath != null && documentNodePath.Node != null && (!DocumentNodeUtilities.IsDynamicResource(documentNodePath.Node) && !DocumentNodeUtilities.IsStaticResource(documentNodePath.Node))) { DocumentNode node2 = documentNodePath.Node; bool preferInPlaceEdit = ControlStylingOperations.CanEditInPlace(targetElement, this.TargetProperty, documentNodePath); ControlStylingOperations.SetActiveEditingContainer(targetElement, this.TargetProperty, node2, documentNodePath, preferInPlaceEdit, editTransaction1); } else { ControlStylingOperations.SetActiveEditingContainer(targetElement, this.TargetProperty, documentNode2, (DocumentNodePath)null, false, editTransaction1); } } editTransaction1.Commit(); } else { editTransaction1.Cancel(); } } } } PerformanceUtility.EndPerformanceSequence(PerformanceEvent.EditStyleOrTemplate); }