Ejemplo n.º 1
0
        private void OnDocumentMonikerChanged(IVsHierarchy hierarchy, string oldMoniker, string newMoniker)
        {
            // If the moniker change only involves casing differences then the project system will
            // not remove & add the file again with the new name, so we should not clear any state.
            // Leaving the old casing in the DocumentKey is safe because DocumentKey equality
            // checks ignore the casing of the moniker.
            if (oldMoniker.Equals(newMoniker, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            // If the moniker change only involves a non-XAML project then ignore it.
            AbstractProject project = GetXamlProject(hierarchy);

            if (project == null)
            {
                return;
            }

            // Managed languages rely on the msbuild host object to add and remove documents during rename.
            // For XAML we have to do that ourselves.
            IVisualStudioHostDocument oldDocument = project.GetCurrentDocumentFromPath(oldMoniker);

            if (oldDocument != null)
            {
                project.RemoveDocument(oldDocument);
            }

            IVisualStudioHostDocument newDocument = project.GetCurrentDocumentFromPath(newMoniker);

            Debug.Assert(newDocument == null, "Why does the renamed document already exist in the project?");
            if (newDocument == null)
            {
                if (TryCreateXamlDocument(project, newMoniker, out newDocument))
                {
                    project.AddDocument(newDocument, isCurrentContext: true, hookupHandlers: true);
                }
            }
        }