【问题标题】:Is there a way to make a DynamicResource be dynamic for a Freezable in a ResourceDictionary?有没有办法让 DynamicResource 对于 ResourceDictionary 中的 Freezable 是动态的?
【发布时间】:2016-01-19 11:13:30
【问题描述】:

我在 Brushes.xaml 中有这个:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:SkinBox.Controls">
    <SolidColorBrush x:Key="{x:Static controls:Keys.BackgroundBrushKey}"
                     Color="{DynamicResource {x:Static controls:Keys.BackgroundColorKey}}" />
</ResourceDictionary>

并在 Generic.xaml 中像这样使用它:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:SkinBox.Controls"
                    xmlns:system="clr-namespace:System;assembly=System">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Skins/Blue.xaml" />
        <ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Brushes.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

问题是 wpf 冻结了画笔,所以 DynamicResource 没有效果。

有没有干净的方法来解决这个问题?我只能想到讨厌的黑客攻击。

【问题讨论】:

    标签: wpf resourcedictionary brush dynamicresource freezable


    【解决方案1】:

    想到一个解决方法:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:controls="clr-namespace:SkinBox.Controls">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Skins/Yellow.Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/SkinBox.Controls;component/Themes/Brushes.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    这仍然不能提供真正的动态资源,但它会重新应用画笔,以便它们在应用皮肤时更新。

    无需在每个皮肤中复制画笔。

    【讨论】:

      猜你喜欢
      • 2012-01-19
      • 2022-01-07
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多