2016-03-06 39 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