我创建了一个简单的项目来演示我遇到错误的问题:'在'System.Windows.Markup.StaticResourceHolder'上提供值引发了异常。' 行号“6”和行位置“9”。
项目布局非常简单,我已将其上传到 dropbox: https ://www.dropbox.com/s/451b5zkw8oqgcld/StyleTest1.zip?dl=0
主窗口.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>
就是这样......如果我运行该程序,我会收到错误消息:'Provide value on 'System.Windows.Markup.StaticResourceHolder' throw an exception。行号“6”和行位置“9”。
但是,如果我将 MainWindow.xaml 更改为以下内容,我将不再遇到问题:这是修改后版本的保管箱链接: https ://www.dropbox.com/s/ceikh5b8cfecdkw/StyleTest2.zip?dl=0
主窗口.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 资源存在问题。
谁能告诉我我在这里做错了什么以及让一个字典中的资源引用另一个字典中的资源的正确方法是什么?
谢谢你的时间,
代码猫头鹰