我创建了一个简单的项目,演示了我遇到错误的问题: '为System.Windows.Markup.StaticResourceHolder'提供价值。抛出异常。'行号' 6'和行位置' 9'。
项目布局非常简单,我已将其上传到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>
那就是它......如果我运行该程序,我会得到错误: &#39;为System.Windows.Markup.StaticResourceHolder&#39;提供价值。抛出异常。&#39;行号&#39; 6&#39;和行位置&#39; 9&#39;。
但是,如果我将MainWindow.xaml更改为以下内容,则不再出现此问题: 以下是修改版本的保管箱链接: 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 :(得分:1)
在Dictionary2中使用DynamicResource
StaticResource
,
或
在Dictionary2中合并词典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>