Example #1
0
        // add all the .xaml dictionaries to the App.xaml resources
        public static void EnsureAppXaml(Project project = null)
        {
            try
            {
                if (project == null)
                {
                    project = VsUtils.GetCurrentProject();
                }

                var appXaml = VsUtils.GetFullPath(project, "App.xaml");
                if (string.IsNullOrEmpty(appXaml))
                {
                    return;                                // should never happen...
                }
                var doc = XDocument.Load(appXaml);

                var appResources       = getNode(doc.Root, "Application.Resources");
                var resDictionary      = getNode(appResources, "ResourceDictionary");
                var mergedDictionaries = getNode(resDictionary, "ResourceDictionary.MergedDictionaries");

                var resFolder = Resourcer.GetResourcesFolderPath();
                if (Directory.Exists(resFolder))
                {
                    // clear all resources
                    mergedDictionaries.RemoveAll();

                    // add them again
                    foreach (var file in VsUtils.GetProjectItemsInFolder(project, resFolder))
                    {
                        // perhaps it's not a dictionary (.xaml) file
                        if (!file.Name.EndsWith(".xaml"))
                        {
                            continue;
                        }
                        // perhaps it is a folder
                        if (!File.Exists(file.Properties.Item("FullPath").Value.ToString()))
                        {
                            continue;
                        }

                        mergedDictionaries.Add(new XElement(
                                                   mergedDictionaries.Name.Namespace + "ResourceDictionary",
                                                   new XAttribute("Source", Settings.ResourcesFolderName + "/" + file.Name)
                                                   ));
                    }
                }

                StringsXMLEditor.SaveDocument(doc, appXaml);
            }
            catch { /* nobody likes errors (which shouldn't happen) :( */ }
        }