コード例 #1
0
        private static void ApplyResourceDictionary(ResourceDictionary newRd, ResourceDictionary oldRd)
        {
            oldRd.BeginInit();

            foreach (DictionaryEntry r in newRd)
            {
                if (oldRd.Contains(r.Key))
                {
                    oldRd.Remove(r.Key);
                }

                oldRd.Add(r.Key, r.Value);
            }

            oldRd.EndInit();
        }
コード例 #2
0
ファイル: StyleLoader.cs プロジェクト: cuilishen/MediaPoint
        public ThemeInfo LoadStyles(string appname, string styleName, ResourceDictionary appDic)
        {
            string path;
            string fileName;

            GetFullPath(appname, styleName, out path, out fileName);

            try
            {
                var themeName = "";
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
                    if (!dic.Contains(THEME_PREFIX))
                    {
                        throw new ThemeException("A theme for MediaPoint needs to have a TextBlock marked with x:Name=\"Theme\" while the text should be the name of the theme.");
                    }
                    themeName = ((TextBlock)dic[THEME_PREFIX]).Text;
                    if (PerformInit)
                    {
                        appDic.BeginInit();
                    }
                    var theme = Application.Current.Resources.MergedDictionaries.FirstOrDefault(rd =>
                    {
                        return(rd.Contains(THEME_PREFIX));
                    });
                    if (theme != null)
                    {
                        appDic.MergedDictionaries.Remove(theme);
                    }
                    appDic.MergedDictionaries.Add(dic);
                    if (PerformInit)
                    {
                        appDic.EndInit();
                    }
                    CurrentStyleFolder = Path.Combine(path, @"Themes\" + styleName);

                    return(new ThemeInfo {
                        Name = themeName, Path = styleName
                    });
                }
            }
            catch (Exception ex)
            {
                throw new ThemeException("Style: " + styleName + " contains an invalid WPF ResourceDictionary.\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.StackTrace);
            }
        }
コード例 #3
0
ファイル: ThemeManager.cs プロジェクト: Tramber/YOMS
        public bool SetCurrentTheme(string name)
        {
            var theme = _themes.FirstOrDefault(x => x.Name == name);

            if (theme == null)
            {
                return(false);
            }

            CurrentTheme = theme;

            if (_applicationResourceDictionary == null)
            {
                _applicationResourceDictionary = new ResourceDictionary();
                Application.Current.Resources.MergedDictionaries.Add(_applicationResourceDictionary);
            }
            _applicationResourceDictionary.BeginInit();
            _applicationResourceDictionary.MergedDictionaries.Clear();

            var windowResourceDictionary = Application.Current.MainWindow.Resources.MergedDictionaries[0];

            windowResourceDictionary.BeginInit();
            windowResourceDictionary.MergedDictionaries.Clear();

            foreach (var uri in theme.ApplicationResources)
            {
                _applicationResourceDictionary.MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = uri
                });
            }

            foreach (var uri in theme.MainWindowResources)
            {
                windowResourceDictionary.MergedDictionaries.Add(new ResourceDictionary
                {
                    Source = uri
                });
            }

            _applicationResourceDictionary.EndInit();
            windowResourceDictionary.EndInit();

            RaiseCurrentThemeChanged(EventArgs.Empty);

            return(true);
        }
コード例 #4
0
ファイル: Flyout.cs プロジェクト: zhangwenyi/MahApps.Metro
        private void OverrideFlyoutResources(ResourceDictionary resources, bool accent = false)
        {
            var fromColorKey = accent ? "MahApps.Colors.Highlight" : "MahApps.Colors.Flyout";

            resources.BeginInit();

            var fromColor = (Color)resources[fromColorKey];

            resources["MahApps.Colors.ThemeBackground"] = fromColor;
            resources["MahApps.Colors.Flyout"]          = fromColor;

            var newBrush = new SolidColorBrush(fromColor);

            newBrush.Freeze();
            resources["MahApps.Brushes.Flyout.Background"]  = newBrush;
            resources["MahApps.Brushes.Control.Background"] = newBrush;
            resources["MahApps.Brushes.ThemeBackground"]    = newBrush;
            resources["MahApps.Brushes.Window.Background"]  = newBrush;
            resources[SystemColors.WindowBrushKey]          = newBrush;

            if (accent)
            {
                fromColor = (Color)resources["MahApps.Colors.IdealForeground"];
                newBrush  = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["MahApps.Brushes.Flyout.Foreground"] = newBrush;
                resources["MahApps.Brushes.Text"] = newBrush;

                if (resources.Contains("MahApps.Colors.AccentBase"))
                {
                    fromColor = (Color)resources["MahApps.Colors.AccentBase"];
                }
                else
                {
                    var accentColor = (Color)resources["MahApps.Colors.Accent"];
                    fromColor = Color.FromArgb(255, accentColor.R, accentColor.G, accentColor.B);
                }

                newBrush = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["MahApps.Colors.Highlight"]  = fromColor;
                resources["MahApps.Brushes.Highlight"] = newBrush;
            }

            resources.EndInit();
        }
コード例 #5
0
        /// <summary>Makes an attempt to load the replacement resources. If failed - re-creates the replacement source</summary>
        private void TryLoadResources()
        {
            ResourceDictionary replacementDictionary = new ResourceDictionary();

            try
            {
                replacementDictionary.BeginInit();
                replacementDictionary.Source = new Uri(replacementSourceFullPath, UriKind.Absolute);
                replacementDictionary.EndInit();

                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { ReplaceAvailableResources(replacementDictionary); }), DispatcherPriority.Send);
            }
            catch (WebException)
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { RestoreReplacementDictionary(); }), DispatcherPriority.Send);
            }
        }
コード例 #6
0
        private void ApplyResourceDictionary(ResourceDictionary oldRd, ResourceDictionary newRd)
        {
            if (oldRd is null)
            {
                throw new ArgumentNullException(nameof(oldRd));
            }

            if (newRd is null)
            {
                throw new ArgumentNullException(nameof(newRd));
            }

            oldRd.BeginInit();

            this.ApplyResourceDictionaryEntries(oldRd, newRd);

            oldRd.EndInit();
        }
コード例 #7
0
        /// <summary>
        /// Convert dictionary to resources
        /// </summary>
        /// <param name="brushes"></param>
        public void SetBrushes(IDictionary <String, SolidColorBrush> brushes)
        {
            ResourceDictionary appDict   = GetAppDictionary();
            ResourceDictionary themeDict = CurrentThemeDictionary;

            // Reset visuals
            // Setup app style
            appDict.BeginInit();
            themeDict.Clear();

            // Object type not known at this point
            foreach (String key in brushes.Keys)
            {
                themeDict[key] = brushes[key];
            }

            appDict.EndInit();
        }
コード例 #8
0
ファイル: App.xaml.cs プロジェクト: zAfLu/Twice
        protected override void OnStartup(StartupEventArgs e)
        {
            if (!SingleInstance.Start())
            {
                SingleInstance.ShowFirstInstance();
                Shutdown(0);
                return;
            }

            DispatcherHelper.Initialize();
            Kernel = new Kernel();

            base.OnStartup(e);
            ConfigureLogging();
            LogTo.Info("Application start");
            LogEnvironmentInfo();

            var conf    = Kernel.Get <IConfig>();
            var palette = new PaletteHelper();

            palette.SetLightDark(conf.Visual.UseDarkTheme);
            palette.ReplaceAccentColor(conf.Visual.AccentColor);
            palette.ReplacePrimaryColor(conf.Visual.PrimaryColor);

            var swatches = new SwatchesProvider().Swatches.ToArray();

            var resDict = new ResourceDictionary();

            resDict.BeginInit();
            {
                resDict.Add("HashtagBrush",
                            new SolidColorBrush(swatches.First(s => s.Name == conf.Visual.HashtagColor).ExemplarHue.Color));
                resDict.Add("LinkBrush",
                            new SolidColorBrush(swatches.First(s => s.Name == conf.Visual.LinkColor).ExemplarHue.Color));
                resDict.Add("MentionBrush",
                            new SolidColorBrush(swatches.First(s => s.Name == conf.Visual.MentionColor).ExemplarHue.Color));
                resDict.Add("GlobalFontSize", (double)conf.Visual.FontSize);
            }
            resDict.EndInit();

            Resources.MergedDictionaries.Add(resDict);

            ChangeLanguage(conf.General.Language);
        }
コード例 #9
0
            private void SetTheme(ResourceDictionary resources, ThemeType newTheme, ThemeType currentTheme)
            {
                if (currentTheme != newTheme)
                {
                    resources.BeginInit();

                    if (currentTheme != ThemeType.none)
                    {
                        RemoveExistingTheme(resources);
                    }

                    if (newTheme != ThemeType.none)
                    {
                        resources.MergedDictionaries.Add(newTheme == ThemeType.light ? lightTheme : darkTheme);
                    }

                    resources.EndInit();
                }
            }
コード例 #10
0
        public void SetLanguage(Lang newLanguage)
        {
            ResourceDictionary dict    = Application.Current.Resources;
            string             resname = "Interface.ru.xaml";

            switch (newLanguage)
            {
            case Lang.Russian:
                resname = "Interface.ru.xaml";
                break;

            case Lang.English:
                resname = "Interface.en.xaml";
                break;

            default:
                resname = "Interface.ru.xaml";
                break;
            }
            try
            {
                dict.BeginInit();
                int i = 0;
                for (i = 0; i < dict.MergedDictionaries.Count; i++)
                {
                    if (((System.Windows.ResourceDictionary)dict.MergedDictionaries[i]).Source.LocalPath.EndsWith(resname))
                    {
                        break;
                    }
                }
                if (i < dict.MergedDictionaries.Count)
                {
                    ResourceDictionary res = dict.MergedDictionaries[i];
                    dict.MergedDictionaries.Remove(dict.MergedDictionaries[i]);
                    dict.MergedDictionaries.Add(res);
                }
            }
            finally
            {
                dict.EndInit();
            }
        }
コード例 #11
0
        public static void ApplyLanguage(string language)
        {
            if (language == "en-US")
            {
                s_stringsDictionary.Source = new Uri(@"Resources\Strings.xaml", UriKind.Relative);
                return;
            }

            if (string.IsNullOrEmpty(language))
            {
                string path = $"{LanguagesDirectory}{Path.DirectorySeparatorChar}Strings.{CultureInfo.CurrentCulture.Name}.xaml";
                if (File.Exists(path))
                {
                    s_stringsDictionary.Source = new Uri(path, UriKind.Absolute);
                }
                else
                {
                    s_stringsDictionary.Source = new Uri(@"Resources\Strings.xaml", UriKind.Relative);
                }
                return;
            }

            foreach (string langPath in Directory.EnumerateFiles(LanguagesDirectory))
            {
                string   file   = Path.GetFileName(langPath);
                string[] tokens = file.Split('.');
                if (tokens.Length < 2)
                {
                    continue;
                }
                if (tokens[1].Contains(language))
                {
                    s_stringsDictionary.BeginInit();
                    s_stringsDictionary.Source = new Uri(langPath, UriKind.Absolute);
                    s_stringsDictionary.EndInit();
                    return;
                }
            }

            s_stringsDictionary.Source = new Uri(@"Resources\Strings.xaml", UriKind.Relative);
        }
コード例 #12
0
ファイル: Flyout.cs プロジェクト: wangfu91/MahApps.Metro
        private void OverrideFlyoutResources(ResourceDictionary resources, bool accent = false)
        {
            var fromColorKey = accent ? "HighlightColor" : "FlyoutColor";

            resources.BeginInit();

            var fromColor = (Color)resources[fromColorKey];

            resources["WhiteColor"]  = fromColor;
            resources["FlyoutColor"] = fromColor;

            var newBrush = new SolidColorBrush(fromColor);

            newBrush.Freeze();
            resources["FlyoutBackgroundBrush"]     = newBrush;
            resources["ControlBackgroundBrush"]    = newBrush;
            resources["WhiteBrush"]                = newBrush;
            resources["WhiteColorBrush"]           = newBrush;
            resources["DisabledWhiteBrush"]        = newBrush;
            resources["WindowBackgroundBrush"]     = newBrush;
            resources[SystemColors.WindowBrushKey] = newBrush;

            if (accent)
            {
                fromColor = (Color)resources["IdealForegroundColor"];
                newBrush  = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["FlyoutForegroundBrush"] = newBrush;
                resources["TextBrush"]             = newBrush;
                resources["LabelTextBrush"]        = newBrush;

                fromColor = (Color)resources["AccentBaseColor"];
                newBrush  = new SolidColorBrush(fromColor);
                newBrush.Freeze();
                resources["HighlightColor"] = fromColor;
                resources["HighlightBrush"] = newBrush;
            }

            resources.EndInit();
        }
コード例 #13
0
        public static void ReplaceDictionary(this ResourceDictionary resourceDictionary, Uri source, Uri destination)
        {
            resourceDictionary.BeginInit();

            if (!resourceDictionary.MergedDictionaries.Any(x => x.Source == destination))
            {
                resourceDictionary.MergedDictionaries.Add(
                    new ResourceDictionary()
                {
                    Source = destination
                });
            }

            ResourceDictionary?oldResourceDictionary = resourceDictionary.MergedDictionaries
                                                       .FirstOrDefault(x => x.Source == source);

            if (oldResourceDictionary != null)
            {
                resourceDictionary.MergedDictionaries.Remove(oldResourceDictionary);
            }

            resourceDictionary.EndInit();
        }
コード例 #14
0
        private Theme ChangeTheme(object?target, ResourceDictionary resourceDictionary, Theme?oldTheme, Theme newTheme)
        {
            if (resourceDictionary is null)
            {
                throw new ArgumentNullException(nameof(resourceDictionary));
            }

            if (newTheme is null)
            {
                throw new ArgumentNullException(nameof(newTheme));
            }

            var themeChanged = false;

            if (oldTheme != newTheme)
            {
                resourceDictionary.BeginInit();

                try
                {
                    ResourceDictionary?       oldThemeDictionary = null;
                    List <ResourceDictionary>?oldThemeResources  = null;

                    if (oldTheme is not null)
                    {
                        oldThemeDictionary = resourceDictionary.MergedDictionaries.FirstOrDefault(d => Theme.GetThemeInstance(d) == oldTheme);

                        if (oldThemeDictionary is null)
                        {
                            oldThemeResources = resourceDictionary.MergedDictionaries.Where(d => Theme.GetThemeName(d) == oldTheme.Name)
                                                .ToList();
                        }
                    }

                    {
                        newTheme.EnsureAllLibraryThemeProvidersProvided();
                        resourceDictionary.MergedDictionaries.Add(newTheme.Resources);

                        //foreach (var themeResource in newTheme.GetAllResources())
                        //{
                        //    // todo: Should we really append the theme resources or try to insert them at a specific index?
                        //    //       The problem here would be to get the correct index.
                        //    //       Inserting them at index 0 is not a good idea as user included resources, like generic.xaml, would be behind our resources.
                        //    //resourceDictionary.MergedDictionaries.Insert(0, themeResource);
                        //    resourceDictionary.MergedDictionaries.Add(themeResource);
                        //}
                    }

                    if (oldThemeDictionary is not null)
                    {
                        resourceDictionary.MergedDictionaries.Remove(oldThemeDictionary);
                    }
                    else if (oldThemeResources is not null)
                    {
                        foreach (var themeResource in oldThemeResources)
                        {
                            resourceDictionary.MergedDictionaries.Remove(themeResource);
                        }
                    }

                    themeChanged = true;
                }
                finally
                {
                    resourceDictionary.EndInit();
                }
            }

            if (themeChanged)
            {
                this.OnThemeChanged(target, resourceDictionary, oldTheme, newTheme);
            }

            return(newTheme);
        }