首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Windows中交叉引用MergedDictionaries?

如何在Windows中交叉引用MergedDictionaries?
EN

Stack Overflow用户
提问于 2013-08-16 16:43:01
回答 2查看 533关注 0票数 1

WPF文档 on MergedDictionaries建议,如果在XAML中加载两个ResourceDictionaries,则第二个加载的ResourceDictionaries将能够引用第一个ResourceDictionariesWindows电话文档 on MergedDictionaries没有说明这一点,但我假设同样的规则也适用。但是,以下内容在我的Windows App.xaml中不起作用,

代码语言:javascript
复制
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="View/ThemeDictionaries/LightThemeResourceDictionary.xaml"/>
    <ResourceDictionary Source="View/ThemeDictionaries/MainResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>

调试说,MainResourceDictionary行不能引用在LightThemeResourceDictionary中定义的SubTitleTextColor --我检查了引用的拼写。

这种类型的加载在Windows中是不可能的吗?我该怎么处理呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-16 17:44:12

基于Silverlight资源文档 (比WPF更适用),这个场景将无法工作。有两种方法可以解决这个问题:

  • 将第一个字典合并到第二个字典中,而不是应用程序的字典。
  • 创建一个附加属性,该属性具有一个List<ResourceDictionary>,该属性将手动将它们合并为一个巨大的ResourceDictionary,并将应用程序的资源设置为它。
票数 1
EN

Stack Overflow用户

发布于 2013-08-18 18:37:48

感谢Abe的帮助,不过最后我还是选择了一个“解析器”类,该类作为合并字典包括在适当的主题字典中。原因超出了原来问题的范围:

  1. 您可以在初始化阶段在App.xaml.cs中加载适当的字典,而不必在以后根据用户的选择以编程方式覆盖资源(在默认主题中合并之前,比如启动时的光照,如果用户选择了Dark,那么稍后在黑暗主题中合并)
  2. 最重要的是,如果您以后重写主题资源,那么在初始化时定义的引用覆盖资源的其他资源不会改变!

代码是基于这个博客作者的想法的。

ThemeResourceDictionaryResolver.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
...

<ResourceDictionary.MergedDictionaries>
    <views:ThemeResourceDictionaryResolver />
</ResourceDictionary.MergedDictionaries>

<!-- StaticResources that refer to the theme StaticResources can go here -->

...

App.xaml

代码语言:javascript
复制
...

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="View/Themes/MainResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>

...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18278268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档