【问题标题】:Change DynamicResource Color Value during runtime在运行时更改 DynamicResource 颜色值
【发布时间】:2013-10-17 12:55:37
【问题描述】:

我的目标是在 Base 主题中定义样式,并能够通过使用 ResourceDictionary 将它们叠加加载来覆盖自定义主题中的某些值。由于Freezable Objects,我已经能够使其与某些属性一起使用,但显然不能与其他属性一起使用,颜色是一种无法正常工作的属性。我认为更改 ResourceDictionary 中的 Color 值会起作用,但它不是:

MainWindow.xaml:

<Grid Background="{DynamicResource PrimaryBackgroundColor}">

Base\Base.xaml:

<SolidColorBrush x:Key="PrimaryBackgroundColor" Color="{DynamicResource Color_Base}"/>

基础\Colors.xaml:

<Color x:Key="Color_Base">Red</Color>

自定义\Colors.xaml:

<Color x:Key="Color_Base">Blue</Color>

主题.cs:

foreach (ResourceDictEntry rde in changeList)
{
    Application.Current.Resources
               .MergedDictionaries
               .ElementAt(rde.dicIndex)[rde.key] = rde.value;
}

当我单步执行时,代码似乎工作正常我看到 Color_Base 的 MergedDictionary 条目从红色 #FFFF0000 更改为蓝色 #FF0000FF

但是,我的背景绑定到 DynamicResource PrimaryBackgroundColor 的网格没有从红色更改为蓝色。

Snoop 中没有显示错误; Grid.Background 值将 PrimaryBackgroundColor 显示为红色 (#FFFF0000)。

我错过了什么?如何在运行时更改颜色值?

这里有一个要点:https://gist.github.com/dirte/773e6baf9a678e7632e6

编辑:

这看起来是最相关的:https://stackoverflow.com/a/17791735/1992193 但我认为样式的全部意义在于在一个地方定义它并让所有东西都使用它,而不必修改后面的每个 xaml/代码?最好的解决方案是什么?

我知道一种解决方案是将整个基本主题简单地复制到自定义主题中,然后只加载您想要的主题,但是它需要跨每个不需要的主题文件管理每个属性。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我能够通过简单地将所有资源字典文件在代码中组合成单个资源字典并应用最终组合的资源字典来制定解决方案。

    public static void ChangeTheme(string themeName)
    {
        string desiredTheme = themeName;    
        Uri uri;
        ResourceDictionary resourceDict;
        ResourceDictionary finalDict = new ResourceDictionary();
    
        // Clear then load Base theme
        Application.Current.Resources.MergedDictionaries.Clear();
        themeName = "Base";
        foreach (var themeFile in Util.GetDirectoryInfo(Path.Combine(ThemeFolder, themeName)).GetFiles())
        {
            uri = new Uri(Util.GetPath(Path.Combine(ThemeFolder, themeName + @"\" + themeFile.Name)));
            resourceDict = new ResourceDictionary { Source = uri };
            foreach (DictionaryEntry de in resourceDict)
            {
                finalDict.Add(de.Key, de.Value);
            }
        }
        // If all you want is Base, we are done
        if (desiredTheme == "Base")
        {
            Application.Current.Resources.MergedDictionaries.Add(finalDict);
            return;
        }
    
        // Now load desired custom theme, replacing keys found in Base theme with new values, and adding new key/values that didn't exist before
        themeName = desiredTheme;
        bool found;
        foreach (var themeFile in Util.GetDirectoryInfo(Path.Combine(ThemeFolder, themeName)).GetFiles())
        {
            uri = new Uri(Util.GetPath(Path.Combine(ThemeFolder, themeName + @"\" + themeFile.Name)));
            resourceDict = new ResourceDictionary { Source = uri };
            foreach (DictionaryEntry x in resourceDict)
            {
                found = false;
                // Replace existing values
                foreach (DictionaryEntry z in finalDict)
                {
                    if (x.Key.ToString() == z.Key.ToString())
                    {
                        finalDict[x.Key] = x.Value;
                        found = true;
                        break;
                    }
                }
    
                // Otherwise add new values
                if (!found)
                {
                    finalDict.Add(x.Key, x.Value);
                }
            }
        }
    
        // Apply final dictionary
        Application.Current.Resources.MergedDictionaries.Add(finalDict);
    }
    

    MainWindow.xaml:

    <Grid Background="{DynamicResource PrimaryBackgroundColor}">
    

    Base.xaml:

    <SolidColorBrush x:Key="PrimaryBackgroundColor" Color="{DynamicResource Color_Base}"/>
    

    基础\Colors.xaml:

    <Color x:Key="Color_Base">Red</Color>
    

    自定义\Colors.xaml:

    <Color x:Key="Color_Base">Blue</Color>
    

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2019-03-07
      • 1970-01-01
      相关资源
      最近更新 更多