我創建了一個簡單的項目,演示我遇到的問題,我得到的錯誤: '在'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
你爲什麼不只是結合了Dictionary1.xaml和Dictionary2.xaml成一個爲了解決這個問題,如果這確實是它的一個根本原因?最好的問候, –
這只是一個演示重現該問題。實際上,我正在爲使用WPF編寫的程序創建附加組件,而Dictionary1實際上是該程序的當前皮膚。所以測試我已經寫了一個WPF應用程序,就像這篇文章中的演示文稿一樣,我在這裏合併了他們的皮膚詞典,就像我在演示中的Dictionary1中合併一樣...然而它不工作......我得到了相同的錯誤...因此,這篇文章... – user2109254
請縮小你的問題到那個特定的編程問題,並刪除過於廣泛的描述性部分。感謝和問候, –