【问题标题】:WPF Merged ResourceDictionary with resource that binds to resource in another Dictionary not workingWPF合并的ResourceDictionary与绑定到另一个字典中的资源的资源不起作用
【发布时间】: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


【解决方案1】:
  1. 在 Dictionary2 中使用 DynamicResource 代替 StaticResource

  2. 在 Dictionary2 中合并 Dictionary 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>

    

【讨论】:

  • 感谢您的回复。但正如问题的cmets所述,我不能。这只是重现问题的演示。实际上,我正在为用 WPF 编写的程序创建一个插件,而 Dictionary1 相当于该程序的当前皮肤。因此,为了测试,我编写了一个 WPF 应用程序,就像这篇文章中的演示一样,我在他们的皮肤字典中合并,就像我在演示中的 Dictionary1 中合并一样......但是它不起作用......我得到了同样的错误......因此这篇文章......
  • 你能用一个图来解释你想做什么吗?
  • 我正在尝试为已经拥有 ResourceDictionary 样式的程序创建一个插件。我想用我的 on 样式扩展他们 ResourceDictionary 中的一些样式。我无法将我的样式添加到他们的资源字典中,因为我的加载项编译成一个 DLL,然后加载到他们的程序中。我的样式将被编译到我的 DLL 中,我无法访问它们的样式,但由于它们支持蒙皮,我知道它们的样式是什么。
  • 所以最终我需要能够在一个 ResourceDictionary 中定义一个引用另一个 ResourceDictionary 中的样式的样式,就像我在原始帖子中尝试做的第一个演示项目一样。根据我所阅读的所有内容,我看不出为什么这不起作用。
  • 太棒了。感谢您花时间解决这个问题。真的很感激!!
猜你喜欢
  • 2017-04-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
相关资源
最近更新 更多