0

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

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>

刷子工作正常,但转换器不行,为什么不呢?

4

2 回答 2

2

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

于 2021-11-17T02:10:57.140 回答
1

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

现代.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>

应用程序.xaml:

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

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

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

于 2019-05-17T14:03:09.840 回答