private static void EnsureType(IType type, ReplaceStyleTemplateCommand.ExtraReferences references)
 {
     if (!type.IsResolvable)
     {
         references.AddUndesolvedType(type);
     }
     else
     {
         if (type.RuntimeAssembly.IsLoaded && ((IPlatformTypes)type.PlatformMetadata).IsDesignToolAssembly(type.RuntimeAssembly))
         {
             return;
         }
         if (PlatformTypes.IsPlatformType((ITypeId)type))
         {
             IAssembly targetAssembly = PlatformTypeHelper.GetTargetAssembly(type);
             if (targetAssembly == null)
             {
                 return;
             }
             string name = targetAssembly.Name;
             if (string.IsNullOrEmpty(name))
             {
                 return;
             }
             references.AddPlatformAssembly(name);
         }
         else
         {
             references.AddAssembly(type.RuntimeAssembly);
         }
     }
 }
        private bool AddReferences(IProjectContext projectContext, ReplaceStyleTemplateCommand.ExtraReferences references, string resourceKey)
        {
            ProjectXamlContext projectXamlContext = ProjectXamlContext.FromProjectContext(projectContext);

            if (projectXamlContext == null)
            {
                return(false);
            }
            if (references.Assemblies.Count > 0)
            {
                ISolution currentSolution = this.DesignerContext.ProjectManager.CurrentSolution;
                if (currentSolution != null)
                {
                    List <IProject>        list1 = new List <IProject>();
                    List <IProjectContext> list2 = new List <IProjectContext>();
                    foreach (IProject project in currentSolution.Projects)
                    {
                        ProjectXamlContext projectContext1 = ProjectXamlContext.GetProjectContext(project);
                        if (projectContext1 != null && projectContext1.ProjectAssembly != null && references.Assemblies.Contains(projectContext1.ProjectAssembly))
                        {
                            list1.Add(project);
                            list2.Add((IProjectContext)projectContext1);
                        }
                    }
                    for (int index = 0; index < list1.Count; ++index)
                    {
                        IProject        source          = list1[index];
                        IProjectContext projectContext1 = list2[index];
                        if (projectXamlContext != projectContext1 && ProjectHelper.DoesProjectReferenceHierarchyContainTarget(source, (IProjectContext)projectXamlContext))
                        {
                            this.DesignerContext.MessageDisplayService.ShowError(string.Format((IFormatProvider)CultureInfo.CurrentCulture, StringTable.DocumentRefersToUnresolvedTypesHeader, new object[1]
                            {
                                (object)resourceKey
                            }));
                            return(false);
                        }
                    }
                }
                foreach (IAssembly assembly in (IEnumerable <IAssembly>)references.Assemblies)
                {
                    projectXamlContext.EnsureAssemblyReferenceMatches(this.SceneViewModel.ProjectContext, assembly);
                }
            }
            foreach (string assemblyPath in (IEnumerable <string>)references.PlatformAssemblies)
            {
                projectXamlContext.EnsureAssemblyReferenced(assemblyPath);
            }
            this.SceneViewModel.DesignerContext.ViewUpdateManager.RebuildPostponedViews();
            return(true);
        }
        private void CollectExtraReferences(DocumentNode node, ReplaceStyleTemplateCommand.ExtraReferences references)
        {
            if (node.Parent != null && node.IsProperty)
            {
                ReplaceStyleTemplateCommand.EnsureMember((IMember)node.SitePropertyKey, references);
            }
            ReplaceStyleTemplateCommand.EnsureType(node.Type, references);
            IMember valueAsMember = DocumentPrimitiveNode.GetValueAsMember(node);

            if (valueAsMember != null)
            {
                ReplaceStyleTemplateCommand.EnsureMember(valueAsMember, references);
            }
            foreach (DocumentNode node1 in node.ChildNodes)
            {
                this.CollectExtraReferences(node1, references);
            }
        }
        private static void EnsureMember(IMember member, ReplaceStyleTemplateCommand.ExtraReferences references)
        {
            IProperty property = member as IProperty;

            if (property != null)
            {
                ReplaceStyleTemplateCommand.EnsureType(property.PropertyType, references);
            }
            IType type = member as IType;

            if (type != null)
            {
                ReplaceStyleTemplateCommand.EnsureType(type, references);
            }
            else
            {
                ReplaceStyleTemplateCommand.EnsureType(member.DeclaringType, references);
            }
        }
 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);
 }