Exemplo n.º 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) :( */ }
        }
Exemplo n.º 2
0
        public static void EnsureResourcesFolder(Project project = null)
        {
            if (project == null)
            {
                project = VsUtils.GetCurrentProject();
            }
            var resFolder = Resourcer.GetResourcesFolderPath();

            if (!Directory.Exists(resFolder)) // if the directory doesn't exist, create it
            {
                Directory.CreateDirectory(resFolder);
            }

            VsUtils.AddDirectoryIfUnexisting(project, resFolder);
        }
        // executed before the menu is open
        void menuCommand_BeforeQueryStatus(object sender, EventArgs e)
        {
            if (sender == null)
            {
                return;
            }

            string filePath;

            VsUtils.GetSingleProjectItemSelection(out filePath);

            var menuCommand = (OleMenuCommand)sender;

            menuCommand.Visible = menuCommand.Enabled = // visible if we selected the resources folder path
                                                        Resourcer.GetResourcesFolderPath().Equals(filePath);
        }
        public static void ShowAddCustomResource()
        {
            // sanitized path
            string       sanitized = string.Empty;
            DialogResult retry;

            do
            {
                retry = DialogResult.No;
                var result = InputTextBox.Show(
                    "Enter the name of the new custom resources XAML file", "Enter a name",
                    sanitized, MAX_PATH_LENGTH - Resourcer.GetResourcesFolderPath().Length - 7);
                // -7 = -(@"\.xaml".Length + 1) // it must be LESS than, not less or equal ^ (so substract 1 extra)

                if (string.IsNullOrEmpty(result))
                {
                    return;
                }

                sanitized = string.Join("_", result.Split(Path.GetInvalidFileNameChars(),
                                                          StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');

                if (!string.IsNullOrWhiteSpace(sanitized))
                {
                    var path = Resourcer.GetXamlResPath(result);
                    if (File.Exists(path))
                    {
                        retry = MessageBox.Show("A file with this name already exists! Do you want to change it?",
                                                "Existing file", MessageBoxButtons.YesNo, MessageBoxIcon.Error);

                        continue;
                    }
                    Resourcer.CreateXamlRes(path, true);
                }
            }while (retry == DialogResult.Yes);
        }