Wpf 如何在Windows Phone中交叉引用合并词典?
Wpf 如何在Windows Phone中交叉引用合并词典?,wpf,xaml,windows-phone-7,resourcedictionary,Wpf,Xaml,Windows Phone 7,Resourcedictionary,MergedDictionaries上的提示是,如果在XAML中加载两个ResourceDictionary,则第二个加载的词典将能够引用第一个词典。关于MergedDictionaries的声明没有明确说明这一点,但我假设同样的规则适用。但是,以下内容在我的Windows Phone App.xaml中不起作用 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="View/ThemeD
MergedDictionaries
上的提示是,如果在XAML中加载两个ResourceDictionary
,则第二个加载的词典将能够引用第一个词典。关于MergedDictionaries
的声明没有明确说明这一点,但我假设同样的规则适用。但是,以下内容在我的Windows Phone App.xaml中不起作用
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/ThemeDictionaries/LightThemeResourceDictionary.xaml"/>
<ResourceDictionary Source="View/ThemeDictionaries/MainResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
调试说MainResourceDictionary行不能引用LightTheMersourceDictionary中定义的SubTitleTextColor
——我已经检查了引用的拼写
在Windows Phone中无法进行这种类型的加载吗?如何解决此问题?基于(比WPF更适用),此方案不起作用。有两种方法可以解决这个问题:
- 将第一个字典合并到第二个字典,而不是应用程序的字典
- 创建一个附加属性,该属性具有一个
,该列表将手动将它们合并到一个巨大的列表
,并将应用程序的资源设置为该属性资源字典
- 您可以在
中的初始化阶段加载相应的词典,而无需稍后根据用户选择以编程方式覆盖资源(在您必须合并到默认主题之前,比如在初始化时使用Light,如果用户选择了Dark,则稍后在Dark主题中合并)App.xaml.cs
- 最重要的是,如果您稍后覆盖了主题资源,那么在初始化时定义的引用被覆盖资源的其他资源将不会更改李>
该代码基于一个
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使用系统; 使用System.IO; 使用System.Windows; 使用System.Windows.Markup; 使用System.Windows.Resources; 使用System.IO.IsolatedStorage; 命名空间YourAppName.View { 公共枚举应用程序 { 黑暗的, 轻的 } 公共类ThemeResourceDictionary Resolver:ResourceDictionary { public ThemeResourceDictionaryResolver():base() { 应用主题; if(System.ComponentModel.DesignerProperties.IsInDesignTool) { //在此处设置要在Blend中加载的词典 主题=应用主题。灯光; } 其他的 { 应用主题; if(!IsolatedStorage.ApplicationSettings.TryGetValue(“ApplicationTheme”,out主题)) { //获得全球手机主题的“标准”方式 可见性DarkbGVvisibility=(可见性)Application.Current.Resources[“PhoneDarkThemeVisibility”]; theme=(darkBGVisibility==可见性.可见)?ApplicationTheme.Dark:ApplicationTheme.Light; } } //根据需要更改URI字符串-这种方式引用字典 //设置为“生成:页面”。设置为“生成为”时,我无法开始工作 //内容,并使用更简单的URI方案。 Uri=newURI(string.Format(“YouAppName;component/View/Themes/{0}ThemeResourceDictionary.xaml”,theme),UriKind.RelativeOrAbsolute); ResourceDictionary res=this.LoadXaml(uri); 此.MergedDictionaries.Add(res); } //出于某种原因,一个简单的ResourceDictionary(){Source=uri};不起作用, //你需要用这个来代替吗 T LoadXaml(Uri) { StreamResourceInfo=Application.GetResourceStream(uri); 如果(信息!=null) { 使用(StreamReader=新StreamReader(info.Stream)) { 返回(T)XamlReader.Load(reader.ReadToEnd()); } } 返回默认值(T); } } }
... <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> ...
Hmm-在文档中的什么地方指定不支持它?在。。。 ...
出现之前,我已经尝试过在c#中执行第二个建议,但是失败了,我现在尝试第一个建议……它没有明确说明不支持此建议,但是关于如何解析资源的描述没有提到搜索“兄弟”词典。InitializeComponent()
- 感谢Abe的帮助,尽管最后我使用了一个“resolver”类,该类作为合并词典包含,并解析为适当的主题词典。原因超出了原问题的范围: