コード例 #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) :( */ }
        }
コード例 #2
0
        // ensure that the App.xaml.cs contains SelectCulture method
        public static void EnsureAppXamlCs(Project project = null)
        {
            if (project == null)
            {
                project = VsUtils.GetCurrentProject();
            }

            var appXamlCs = VsUtils.GetFullPath(project, "App.xaml.cs");

            if (string.IsNullOrEmpty(appXamlCs))
            {
                return;                                  // should never happen...
            }
            var source = File.ReadAllText(appXamlCs, Encoding.UTF8);

            var startRegex = new Regex(@"class\s*App\s*:\s*Application\s*{");
            var start      = startRegex.Match(source);

            if (!start.Success)
            {
                return;                 // should never happen either!...
            }
            var startIdx = start.Index + start.Length;

            if (!source.Contains(selectCultureMethodCheck)) // make sure to add the method
            {
                var sb = new StringBuilder();

                foreach (var rusing in requiredAppUsings)
                {
                    if (!source.Contains(rusing))
                    {
                        sb.AppendLine(rusing);
                    }
                }

                appendAfterStart(sb, startIdx, ref source, selectCultureMethod);
                File.WriteAllText(appXamlCs, source);
            }

            if (Settings.SetLocaleOnStartup && !source.Contains(selectCultureMethodCall))
            {
                var constructorMatch = new Regex(@"public\s+App\s*\(\)\s*{");
                var constructor      = constructorMatch.Match(source);

                var sb = new StringBuilder();
                if (constructor.Success) // append only the method call
                {
                    var constructorIdx = constructor.Index + constructor.Length;

                    // indentation = 3 indent levels * 4 spaces each
                    appendAfterStart(sb, constructorIdx, ref source,
                                     new string(' ', 3 * 4) + selectCultureMethodCall);

                    File.WriteAllText(appXamlCs, source);
                }
                else // append a whole new constructor
                {
                    appendAfterStart(sb, startIdx, ref source, selectCultureConstructor);
                    File.WriteAllText(appXamlCs, source);
                }
            }
            else if (source.Contains(selectCultureMethodCall))
            {
                if (source.Contains(selectCultureMethodCall))
                {
                    source = source.Replace(selectCultureMethodCall, string.Empty);
                    File.WriteAllText(appXamlCs, source);
                }
            }
            if (!source.Contains(initializeComponent))
            {
                var constructorMatch = new Regex(@"public\s+App\s*\(\)\s*{");
                var constructor      = constructorMatch.Match(source);

                var sb = new StringBuilder();
                if (constructor.Success) // append only the method call
                {
                    var constructorIdx = constructor.Index + constructor.Length;

                    // indentation = 3 indent levels * 4 spaces each
                    appendAfterStart(sb, constructorIdx, ref source,
                                     new string(' ', 3 * 4) + initializeComponent);

                    File.WriteAllText(appXamlCs, source);
                }
                else // append a whole new constructor
                {
                    appendAfterStart(sb, startIdx, ref source, selectCultureConstructor);
                    File.WriteAllText(appXamlCs, source);
                }
            }
        }