【发布时间】:2016-03-06 03:26:24
【问题描述】:
我创建了一个简单的项目来演示我遇到错误的问题: '在 '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>
就是这样......如果我运行该程序,我会收到错误: '在 '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 中合并一样......但是它不起作用......我得到了同样的错误......因此这篇文章......
-
请将您的问题缩小到特定的编程问题,并删除过于宽泛的描述性部分。谢谢和问候,
-
@AlexBell,不知道你是什么意思,伙计。基本上,当我在一个资源字典中定义一个引用第二个资源字典中资源的资源时,我得到了问题中提到的错误,这个问题是如何解决这个问题?
-
@AlexBell,虽然我很欣赏这个建议,但我不能那样做。正如我所说的 Dictionary1 是一个皮肤文件。用户可以制作自己的皮肤,当他们这样做时,他们不会有我的样式。我正在创建使用主皮肤中的一些样式的样式,因此我可以将它们分配给我的附加组件,但也包括我的设计。通过这种方式,我可以保留文本前景和背景颜色等的当前皮肤设置......所以我的插件将匹配当前皮肤,但我的样式也有只适用于我的设计的东西。跨度>
标签: wpf xaml mergeddictionaries