有没有办法让DynamicResource在ResourceDictionary中为Freezable动态化?

时间:2015-10-20 17:07:33

标签: wpf resourcedictionary brush dynamicresource freezable

我在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无效。

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

1 个答案:

答案 0 :(得分:0)

考虑解决方法:

<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>

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

无需在每张皮肤上复制画笔。