【问题标题】:Exception: Cannot find resource named '*'. Resource names are case sensitive例外:找不到名为“*”的资源。资源名称区分大小写
【发布时间】:2019-05-17 13:16:46
【问题描述】:

但是为什么。我认为文件的工作方式是正确的:

IFocus.xaml 中的错误,但之前定义了转换器。 我不明白出了什么问题。

参考:Modern.xaml 是来自另一个项目的引用。我喜欢这个。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:c="clr-namespace:Modern.Converters">

<SolidColorBrush x:Key="C_FocusBush" Color="Red"/>
<c:ThicknessConverter x:Key="ThicknessConverter"/>

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Interfaces/IFocus/IFocus.xaml"/>
</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

IFocus.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:i="clr-namespace:Modern.Interfaces">
<Style TargetType="{x:Type i:IFocus}" x:Key="{x:Type i:IFocus}">
    <Setter Property="BorderBrush" Value="{DynamicResource C_FocusBush}"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="Padding" Value="2"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type i:IFocus}">

                <Grid Margin="{TemplateBinding Padding}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessConverter}"/>
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

</ResourceDictionary>

包含所有资源的主应用程序:

<Application x:Class="*.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Modern;component/Modern.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

</Application>

画笔可以正常工作,但转换器不行,为什么不呢?

【问题讨论】:

  • 您使用StaticResource 检索转换器,但DynamicResource 检索画笔。猜测是,在定义任何一个资源之前的某个时间点解析 XAML。 DynamicResource 不介意;当值可用时,它会保留并提供一个值(并保留在场景中,以防值发生变化)。 StaticResource 必须在那时提供一个值,但它不能。
  • ResourceDictionary.MergedDictionaries 出现在 XAML 文件文本中这两个资源定义的下方,但我会犹豫是否要保证 XAML 中的定义实际上是按该顺序解析的。
  • 我怎样才能做到这一点?奇怪的行为。
  • 如果这些资源特定于该样式,请将它们放在同一个 XAML 文件中。如果它们是广泛使用的“主题”资源,请使用DynamicResource 检索它们。

标签: wpf new-project


【解决方案1】:

在将 Visual Studio 主题更改为自定义(我的 OneMonokai)后,我遇到了与此相同的错误格式。我只是将其恢复为默认主题,令人惊讶的是它已解决。这似乎与问题相去甚远,但如果您愿意,可以尝试此修复。

【讨论】:

  • 将 DarkGreen 改为 Light 为我解决了这个问题。这个很奇怪。不过,Kelvin,还是谢谢你。
【解决方案2】:

由于IFocus.xaml 中的Style 引用了Modern.xaml 中的Brush 资源,因此IFocus.xaml 应该合并Modern.xaml,而不是相反:

Modern.xaml:

<ResourceDictionary ...>
    <SolidColorBrush x:Key="C_FocusBush" Color="Red"/>

</ResourceDictionary>

IFocus.xaml:

<ResourceDictionary ...>
    <Style ...>
        <Setter Property="BorderBrush" Value="{StaticResource C_FocusBush}"/>
    </Style>

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../../Modern.xaml"/>
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

App.xaml:

<ResourceDictionary Source="pack://application:,,,/Modern;component/Interfaces/IFocus/IFocus.xaml"/>

或者,您可以创建一个包含所有画笔的单独资源字典,并将这个和一个带有样式的资源字典合并到 App.xaml 或另一个资源字典中。

您可能还想查看我的回答 here,了解有关资源字典加载顺序的更多信息。

【讨论】:

  • 难道不是另一种在引用中合并 xaml 并在 App 中仅包含一个 xaml 的方法吗?
  • 我不确定我理解你的意思。您不应该在 A 中定义引用 B 中的资源的样式,而无需将 B 合并到 A 中,因为 A 确实依赖于 B。如果不想合并,直接在A中定义资源即可。
  • 或者在单独的字典中定义画笔并将它们合并到App.xaml
  • 喜欢。 A -> 合并:画笔、IFocus 等...,B -> 合并 A
  • 如果您按顺序将BrushesIFocus 合并到App.xaml,它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 2018-01-05
  • 2012-10-31
  • 1970-01-01
相关资源
最近更新 更多