我有一個ResourceDictionary,其中包含一個Brush對象和一個使用此Brush對象的模板屬性(通過StaticResource標記擴展)中的幾個動畫屬性的樣式。問題是;當我將字典與全局應用程序ResourceDictionary(Application.Resources)合併時,Brush不會被凍結,並且每個共享Style的元素都會受到對Brush的更改的影響。有趣的是,當我將筆刷移動到輔助合併的ResourceDictionary時,它被凍結,一切按預期工作(在動畫之前,凍結被克隆)只有當一個可凍結的對象和其他資源引用這個對象時纔會出現問題通過StaticResource標記擴展駐留在相同的合併ResourceDictionary中。我粘貼下面的App.xaml,Window.xaml和Dictionary.xaml的示例代碼。如果您能重現相同的結果並確認這是WPF中的錯誤,我將不勝感激。WPF Merged ResourceDictionary inconsistencies
注意:如果在Visual Studio中將ResourceDictionary(Dictionary.xaml)的內容類型從頁面更改爲資源(並將其嵌入到已編譯程序集的XAML而不是BAML版本),問題就會消失。
Window.xaml
<Window x:Class="WpfApplication1.Window" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300">
<StackPanel>
<Button Height="30" Content="Test 1" Margin="5" />
<Button Height="30" Content="Test 2" Margin="5" />
</StackPanel>
的App.xaml
<Application x:Class="WpfApplication.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Dictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="backgroundBrush" Color="Aqua" />
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="border" Background="{StaticResource backgroundBrush}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Opacity" To="0" Duration="0:0:.2" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Opacity" To="1" Duration="0:0:.2" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>