XamlParseException :: 找不到名为“DefaultStyle”的资源
XamlParseException :: Cannot find resource named 'DefaultStyle`
我定制了一个 togglebutton
用于控制箱。但是当我将鼠标悬停在它上面时它抛出了这个异常:
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.'
内部异常:
'Cannot find resource named 'DefaultStyle'. Resource names are case sensitive.
这有点奇怪,因为在我的整个项目中没有命名或使用名称 DefaultStyle
!
经过一番挖掘后,我注意到错误出现延迟,所以我记得这次我添加了一个 tooltip
,这似乎是导致问题的原因,但我不明白为什么!
这是XAML:
<Style x:Key="ToggleClose" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid x:Name="_Container" Background="#00000000">
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Path x:Name="_Icon" SnapsToDevicePixels="True" ToolTip="Close window" Width="18" Height="18" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Fill" Stroke="#4D4D4D" StrokeThickness="1" Data="M0,0 L1,1 M0,1 L1,0" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="_Container" Property="Background" Value="#80FF0000" />
<Setter TargetName="_Icon" Property="Stroke" Value="#2D2D2D" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="local:WindowBehaviours.Close" Value="True" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
编辑:
好的,似乎任何 tooltip
都会抛出异常,而不仅仅是 togglebutton
,即使是非常不寻常的 UI Debugging Tools for XAML
。
编辑 2:
这些是我正在使用的资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Objects/Customviewer.xaml"/>
<ResourceDictionary Source="Resources/Objects/CustomScroller.xaml"/>
<ResourceDictionary Source="Resources/Objects/CustomButtons.xaml"/>
<ResourceDictionary Source="Resources/Rooks/Alising.xaml"/>
<ResourceDictionary Source="Resources/Themes/VisualOne.xaml"/>
<ResourceDictionary Source="Resources/Themes/VisualDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
编辑 3:
<Popup>
也有同样的问题,抛出相同的异常。
它在我这里工作正常...我猜你在获取 'DefaultStyle' 时遗漏了一些东西。
XAML-
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Style/Styles.xaml"/>
<ResourceDictionary x:Name="DefaultStyle"
Source="Dictionary1.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
所有你提到的 XAML 代码都在我的 'Dictionary1.xaml'
然后我在...
中使用它
<ToggleButton Name="CloseButton" Style="{StaticResource ToggleClose}" Grid.Row="1" Grid.Column="1" />
同时检查您的字典和 WindowBehaviours class 没有错误。如果您仍然有问题,请告诉我。
问题出在自动生成的资源文件本身,它不知何故被破坏了。删除它并重新生成一个新的解决了问题。
我定制了一个 togglebutton
用于控制箱。但是当我将鼠标悬停在它上面时它抛出了这个异常:
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.'
内部异常:
'Cannot find resource named 'DefaultStyle'. Resource names are case sensitive.
这有点奇怪,因为在我的整个项目中没有命名或使用名称 DefaultStyle
!
经过一番挖掘后,我注意到错误出现延迟,所以我记得这次我添加了一个 tooltip
,这似乎是导致问题的原因,但我不明白为什么!
这是XAML:
<Style x:Key="ToggleClose" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid x:Name="_Container" Background="#00000000">
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Path x:Name="_Icon" SnapsToDevicePixels="True" ToolTip="Close window" Width="18" Height="18" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Fill" Stroke="#4D4D4D" StrokeThickness="1" Data="M0,0 L1,1 M0,1 L1,0" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="_Container" Property="Background" Value="#80FF0000" />
<Setter TargetName="_Icon" Property="Stroke" Value="#2D2D2D" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="local:WindowBehaviours.Close" Value="True" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
编辑:
好的,似乎任何 tooltip
都会抛出异常,而不仅仅是 togglebutton
,即使是非常不寻常的 UI Debugging Tools for XAML
。
编辑 2:
这些是我正在使用的资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Objects/Customviewer.xaml"/>
<ResourceDictionary Source="Resources/Objects/CustomScroller.xaml"/>
<ResourceDictionary Source="Resources/Objects/CustomButtons.xaml"/>
<ResourceDictionary Source="Resources/Rooks/Alising.xaml"/>
<ResourceDictionary Source="Resources/Themes/VisualOne.xaml"/>
<ResourceDictionary Source="Resources/Themes/VisualDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
编辑 3:
<Popup>
也有同样的问题,抛出相同的异常。
它在我这里工作正常...我猜你在获取 'DefaultStyle' 时遗漏了一些东西。
XAML-
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Style/Styles.xaml"/>
<ResourceDictionary x:Name="DefaultStyle"
Source="Dictionary1.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
所有你提到的 XAML 代码都在我的 'Dictionary1.xaml' 然后我在...
中使用它 <ToggleButton Name="CloseButton" Style="{StaticResource ToggleClose}" Grid.Row="1" Grid.Column="1" />
同时检查您的字典和 WindowBehaviours class 没有错误。如果您仍然有问题,请告诉我。
问题出在自动生成的资源文件本身,它不知何故被破坏了。删除它并重新生成一个新的解决了问题。