例外:找不到名为“ *”的资源。资源名称区分大小写

时间:2019-05-17 13:16:47

标签: wpf new-project

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

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>

画笔工作正常,但转换器无法正常工作,为什么不呢?

1 个答案:

答案 0 :(得分:2)

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

Modern.xaml:

Modern.xaml

IFocus.xaml:

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

</ResourceDictionary>

App.xaml:

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

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

</ResourceDictionary>

或者,您可以创建一个包含所有画笔的单独的资源字典,并将这一个和具有样式的画笔合并到<ResourceDictionary Source="pack://application:,,,/Modern;component/Interfaces/IFocus/IFocus.xaml"/> 或另一资源字典中。

您可能还希望查看我的答案here,以获取有关资源字典加载顺序的更多信息。