积极答复者
UWP 动态加载Resources问题

问题
-
定义的Resources资源汇集到APP.xaml里,都是静态的,要动态的只能如此
ResourceDictionary newResourceDictionary = new ResourceDictionary(); newResourceDictionary.Source = new Uri("ms-appx:///Theme/ThemeDark.xaml", UriKind.RelativeOrAbsolute); //newResourceDictionary_1.Source = new Uri("ms-appx:///Theme/StyleResources.xaml", UriKind.RelativeOrAbsolute); Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary);
如果要加载很多的XAML文件怎么办?我自己实现了下面的方法有时候有效有时候又无效,真的好奇怪,
public void LadingXaml() { ICollection<ResourceDictionary> tempRE = Application.Current.Resources.MergedDictionaries; Application.Current.Resources.MergedDictionaries.Clear(); foreach (var item in tempRE) { Application.Current.Resources.MergedDictionaries.Add(item); } //await new MessageDialog(tempRE.Count.ToString()).ShowAsync(); }
答案
-
#region 新测试 string tempTD = "ms-appx:///Theme/ThemeDark.xaml"; string tempTL = "ms-appx:///Theme/ThemeLight.xaml"; string tempSR = "ms-appx:///Theme/StyleResources.xaml"; var res = new ResourceDictionary(); var appRs = Application.Current.Resources.MergedDictionaries; if (LightOrDark==false) { appRs.Clear(); res.Source = new Uri(tempTD); appRs.Add(res); } else { appRs.Clear(); res.Source = new Uri(tempTL); appRs.Add(res); } #endregion
已经解决了
再次感谢,您的帮助,有时候一个问题转了牛角尖了,把简单的问题搞复杂了, - -|||
- 已标记为答案 Enious 2016年5月27日 15:42
全部回复
-
ResourceDictionary.CopyTo 方法
找到这个,但是有个重点这个方法:如果再次重建为 ResourceDictionary,则结果数组中将没有 MergedDictionaries。
ResourceDictionary[] tempRE = new ResourceDictionary[] { };
//await new MessageDialog((tempRE.Count<ResourceDictionary>().ToString())).ShowAsync();
Application.Current.Resources.MergedDictionaries.CopyTo(tempRE , 0);会爆The specified index is out of bounds of the specified array
- 已编辑 Enious 2016年5月27日 5:19 重新编辑
-
刚刚试了下你的代码,不知道转成C#是不是对的.会报异常 An item with the same key has already been added.
List<string> flies = new List<string> { "ms-appx:///Theme/StyleResources.xaml","ms-appx:///Theme/ThemeDark.xaml" ,"ms-appx:///Theme/ThemeLight.xaml" }; var res = new ResourceDictionary(); var appRs = Application.Current.Resources; foreach (var file in flies) { res.Clear(); Application.LoadComponent(res, new Uri(file), ComponentResourceLocation.Application); foreach (var item in res) { appRs.Add(item.Key,item.Value); } }
-
还是有点问题标题栏颜色改变不了,默认Light主题,切到Dark主题标题栏颜色还是Light的,我最开始的代码在调用处使用完全没这个问题.
调用处
#region 日间or夜间模式 if (RequestedTheme == ElementTheme.Light) { #region 另外一种载入 //newResourceDictionary = new ResourceDictionary(); //newResourceDictionary.Source = new Uri("ms-appx:///Theme/ThemeDark.xaml", UriKind.RelativeOrAbsolute); //Application.Current.Resources.MergedDictionaries.Clear(); //Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary); #endregion LightOrDark = false; loadr.LadingXaml(LightOrDark); L_DTxt.Text = "日间模式"; L_DFontIcon.Glyph = "\uE706";//该图标颜色为黑色月亮 RequestedTheme = ElementTheme.Dark; } else { #region 另外一种载入 //newResourceDictionary = new ResourceDictionary(); ////newResourceDictionary.Source = new Uri("ms-appx:///Theme/ThemeDark.xaml", UriKind.RelativeOrAbsolute); //Application.Current.Resources.MergedDictionaries.Clear(); //Application.Current.Resources.MergedDictionaries.Add(newResourceDictionary); #endregion LightOrDark = true; loadr.LadingXaml(LightOrDark); L_DTxt.Text = "夜间模式"; L_DFontIcon.Glyph = "\uE708";//该图标颜色为白色太阳 RequestedTheme = ElementTheme.Light; } ChangTitBarColor(); #endregion break;
方法体里面
#region 新测试 string tempTD = "ms-appx:///Theme/ThemeDark.xaml"; string tempTL = "ms-appx:///Theme/ThemeLight.xaml"; string tempSR = "ms-appx:///Theme/StyleResources.xaml"; var res = new ResourceDictionary(); var appRs = Application.Current.Resources; if (LightOrDark==false) { res.Clear(); Application.LoadComponent(res, new Uri(tempTD,UriKind.RelativeOrAbsolute), ComponentResourceLocation.Application); foreach (var item in res) { appRs.Add(item.Key, item.Value); } } else { res.Clear(); Application.LoadComponent(res, new Uri(tempTL,UriKind.RelativeOrAbsolute), ComponentResourceLocation.Application); foreach (var item in res) { appRs.Add(item.Key, item.Value); } } #endregion
还是会报An item with the same key has already been added.
-
#region 新测试 string tempTD = "ms-appx:///Theme/ThemeDark.xaml"; string tempTL = "ms-appx:///Theme/ThemeLight.xaml"; string tempSR = "ms-appx:///Theme/StyleResources.xaml"; var res = new ResourceDictionary(); var appRs = Application.Current.Resources.MergedDictionaries; if (LightOrDark==false) { appRs.Clear(); res.Source = new Uri(tempTD); appRs.Add(res); } else { appRs.Clear(); res.Source = new Uri(tempTL); appRs.Add(res); } #endregion
已经解决了
再次感谢,您的帮助,有时候一个问题转了牛角尖了,把简单的问题搞复杂了, - -|||
- 已标记为答案 Enious 2016年5月27日 15:42