wpf 切换资源字典的2中方式

var _1200RDUri = new Uri(String.Format(@"/aa;Component/Themes/1200Theme.xaml"), UriKind.RelativeOrAbsolute);
            var _980RDUri = new Uri(String.Format(@"/aa;Component/Themes/980Theme.xaml"), UriKind.RelativeOrAbsolute);
            var _1200RD = Application.LoadComponent(_1200RDUri) as ResourceDictionary;
            var _980RD = Application.LoadComponent(_980RDUri) as ResourceDictionary;
            if (_1200RD == null || _980RD == null)
            {
                return;
            }
            if (!IsScreenWidthAbove1200)
            {
                Application.Current.Resources.MergedDictionaries.Remove(_1200RD);
                Application.Current.Resources.MergedDictionaries.Remove(_980RD);
                Application.Current.Resources.MergedDictionaries.Add(_980RD);
            }
            else
            {
                Application.Current.Resources.MergedDictionaries.Remove(_1200RD);
                Application.Current.Resources.MergedDictionaries.Remove(_980RD);
                Application.Current.Resources.MergedDictionaries.Add(_1200RD);
            }

 

 private static void ChangeAppStyle(ResourceDictionary resources, Tuple<AppTheme, Accent> oldThemeInfo, Accent newAccent, AppTheme newTheme)
        {
            var themeChanged = false;
            if (oldThemeInfo != null)
            {
                var oldAccent = oldThemeInfo.Item2;
                if (oldAccent != null && oldAccent.Name != newAccent.Name)
                {
                    var key = oldAccent.Resources.Source.ToString().ToLower();
                    var oldAccentResource = resources.MergedDictionaries.Where(x => x.Source != null).FirstOrDefault(d => d.Source.ToString().ToLower() == key);
                    if (oldAccentResource != null)
                    {
                        resources.MergedDictionaries.Add(newAccent.Resources);
                        resources.MergedDictionaries.Remove(oldAccentResource);

                        themeChanged = true;
                    }
                }

                var oldTheme = oldThemeInfo.Item1;
                if (oldTheme != null && oldTheme != newTheme)
                {
                    var key = oldTheme.Resources.Source.ToString().ToLower();
                    var oldThemeResource = resources.MergedDictionaries.Where(x => x.Source != null).FirstOrDefault(d => d.Source.ToString().ToLower() == key);
                    if (oldThemeResource != null)
                    {
                        resources.MergedDictionaries.Add(newTheme.Resources);
                        resources.MergedDictionaries.Remove(oldThemeResource);

                        themeChanged = true;
                    }
                }
            }
            else
            {
                ChangeAppStyle(resources, newAccent, newTheme);

                themeChanged = true;
            }

            if (themeChanged)
            {
                OnThemeChanged(newAccent, newTheme);
            }
        }
这段不能直接用,得看一下代码意思

Application.Current.Resources
 Resources = new ResourceDictionary {Source = resourceAddress};

 

posted @ 2016-11-22 11:33  莫欺  阅读(420)  评论(0编辑  收藏  举报