【发布时间】: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