WPF文档 on MergedDictionaries建议,如果在XAML中加载两个ResourceDictionaries,则第二个加载的ResourceDictionaries将能够引用第一个ResourceDictionaries。Windows电话文档 on MergedDictionaries没有说明这一点,但我假设同样的规则也适用。但是,以下内容在我的Windows App.xaml中不起作用,
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/ThemeDictionaries/LightThemeResourceDictionary.xaml"/>
<ResourceDictionary Source="View/ThemeDictionaries/MainResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>调试说,MainResourceDictionary行不能引用在LightThemeResourceDictionary中定义的SubTitleTextColor --我检查了引用的拼写。
这种类型的加载在Windows中是不可能的吗?我该怎么处理呢?
发布于 2013-08-16 17:44:12
基于Silverlight资源文档 (比WPF更适用),这个场景将无法工作。有两种方法可以解决这个问题:
List<ResourceDictionary>,该属性将手动将它们合并为一个巨大的ResourceDictionary,并将应用程序的资源设置为它。发布于 2013-08-18 18:37:48
感谢Abe的帮助,不过最后我还是选择了一个“解析器”类,该类作为合并字典包括在适当的主题字典中。原因超出了原来问题的范围:
App.xaml.cs中加载适当的字典,而不必在以后根据用户的选择以编程方式覆盖资源(在默认主题中合并之前,比如启动时的光照,如果用户选择了Dark,那么稍后在黑暗主题中合并)代码是基于这个博客作者的想法的。
ThemeResourceDictionaryResolver.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Resources;
using System.IO.IsolatedStorage;
namespace YourAppName.View
{
public enum ApplicationTheme
{
Dark,
Light
}
public class ThemeResourceDictionaryResolver : ResourceDictionary
{
public ThemeResourceDictionaryResolver() : base()
{
ApplicationTheme theme;
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
// Set here which dictionary you want to be loaded in Blend
theme = ApplicationTheme.Light;
}
else
{
ApplicationTheme theme;
if (!IsolatedStorage.ApplicationSettings.TryGetValue("ApplicationTheme", out theme))
{
// The 'standard' way to get the global phone theme
Visibility darkBGVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
theme = (darkBGVisibility == Visibility.Visible) ? ApplicationTheme.Dark : ApplicationTheme.Light;
}
}
// Change the URI string as appropriate - this way refers to the Dictionaries
// which are set to 'Build: Page'. I couldn't get to work when set to Build as
// Content and using the simpler URI scheme.
Uri uri = new Uri(string.Format("YouAppName;component/View/Themes/{0}ThemeResourceDictionary.xaml", theme), UriKind.RelativeOrAbsolute);
ResourceDictionary res = this.LoadXaml<ResourceDictionary>(uri);
this.MergedDictionaries.Add(res);
}
// For some reason a simple ResourceDictionary() { Source = uri }; does not work,
// need to use this instead
T LoadXaml<T>(Uri uri)
{
StreamResourceInfo info = Application.GetResourceStream(uri);
if (info != null)
{
using (StreamReader reader = new StreamReader(info.Stream))
{
return (T)XamlReader.Load(reader.ReadToEnd());
}
}
return default(T);
}
}
}MainResourceDictionary.xaml
...
<ResourceDictionary.MergedDictionaries>
<views:ThemeResourceDictionaryResolver />
</ResourceDictionary.MergedDictionaries>
<!-- StaticResources that refer to the theme StaticResources can go here -->
...App.xaml
...
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/Themes/MainResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
...https://stackoverflow.com/questions/18278268
复制相似问题