2016-03-06 84 views
0

我创建了一个简单的项目,演示我遇到的问题,我得到的错误: '在'System.Windows.Markup.StaticResourceHolder'上提供值'抛出一个例外。“行号“6”和行位置“9”。WPF合并ResourceDictionary与资源绑定到另一个字典中的资源不工作

项目布局非常简单,我把它上传到Dropbox的: https://www.dropbox.com/s/451b5zkw8oqgcld/StyleTest1.zip?dl=0

MainWindow.xaml

<Window x:Class="StyleTest1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Dictionary1.xaml" /> 
       <ResourceDictionary Source="Dictionary2.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
      <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
       <Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/> 
      </Style> 
     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}"> 

     </Button> 
    </Grid> 
</Window> 

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <GradientStopCollection po:Freeze="true" x:Key="ButtonBackgroundStops"> 
     <GradientStop Color="#2d2d2f"/> 
     <GradientStop Color="#2d2d2f" Offset="1"/> 
    </GradientStopCollection> 

    <LinearGradientBrush 
     po:Freeze="true"  
     x:Key="ButtonBackgroundBrush"   
     GradientStops="{StaticResource ButtonBackgroundStops}" 
     StartPoint="0.5,-0.05" 
     EndPoint="0.5,0.66" /> 

</ResourceDictionary> 

Dictionary2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <LinearGradientBrush 
     x:Key="Button.Static.Background" 
     GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}" 
     StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" 
     EndPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=EndPoint}"/> 

</ResourceDictionary> 

就是这样...如果我运行该程序,我得到的错误: '在'System.Windows.Markup.StaticResourceHolder'上提供值引发了一个异常。'行号“6”和行位置“9”。

但是,如果我改变MainWindow.xaml到下面我不再获得问题: 这里是Dropbox的链接,修改后的版本: https://www.dropbox.com/s/ceikh5b8cfecdkw/StyleTest2.zip?dl=0

MainWindow.xaml

<Window x:Class="StyleTest2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:StyleTest2" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Dictionary1.xaml" /> 
       <ResourceDictionary Source="Dictionary2.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
      <LinearGradientBrush 
       x:Key="Button.Static.Background" 
       GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}" 
       StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" /> 
      <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
       <Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/> 
      </Style> 
     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}"> 

     </Button> 
    </Grid> 
</Window> 

哪会建议它与Dictionary2.xaml中的LinearGradientBrush绑定到位于Dictionary1.xaml中的ButtonBackgroundBrush资源时出现问题。

任何人都可以告诉我我在做什么错在这里,什么是正确的方法来在一个字典中的资源引用另一个字典中的资源?

感谢您的时间,

codeOwl

+0

你为什么不只是结合了Dictionary1.xaml和Dictionary2.xaml成一个为了解决这个问题,如果这确实是它的一个根本原因?最好的问候, –

+0

这只是一个演示重现该问题。实际上,我正在为使用WPF编写的程序创建附加组件,而Dictionary1实际上是该程序的当前皮肤。所以测试我已经写了一个WPF应用程序,就像这篇文章中的演示文稿一样,我在这里合并了他们的皮肤词典,就像我在演示中的Dictionary1中合并一样...然而它不工作......我得到了相同的错误...因此,这篇文章... – user2109254

+0

请缩小你的问题到那个特定的编程问题,并删除过于广泛的描述性部分。感谢和问候, –

回答

1
  1. 使用DynamicResource就地在Dictionary2 StaticResource

    或Dictionary2

  2. 合并字典1,那么就赢了没有任何问题。

Dictionary2看起来像:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Dictionary1.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 

    <LinearGradientBrush 
     x:Key="Button.Static.Background" 
     GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}" 
     StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" 
     EndPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=EndPoint}"/> 

</ResourceDictionary> 
+0

感谢您的回复。但正如问题的评论所述,我不能。这只是一个重现问题的演示。实际上,我正在为使用WPF编写的程序创建一个附加组件,Dictionary1与该程序的当前皮肤相当。所以测试我已经写了一个WPF应用程序,就像这篇文章中的演示文稿一样,我在这里合并了他们的皮肤词典,就像我在演示中的Dictionary1中合并一样...然而它不工作......我得到了同样的错误...因此,这篇文章... – user2109254

+0

你可以解释一下你想要做什么? – AnjumSKhan

+0

我正在尝试为已经具有样式的ResourceDictionary的程序创建附加组件。我想用我的样式来扩展其ResourceDictionary中的一些样式。我无法将我的样式添加到他们的资源字典中,因为我的加载项编译为一个随后加载到其程序中的DLL。我的样式将被编译到我的DLL中,我没有访问他们的样式,但是因为他们支持蒙皮,我知道他们的样式是什么。 – user2109254