我已将我的共享样式放入一个包含在应用程序中的资源字典中。例如,在一个名为GlobalStyles.xaml
我有以下几点:资源字典中的出界错误
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="BaseStyle" TargetType="FrameworkElement">...</Style>
<Style x:Key="ExtendedStyle" TargetType="FrameworkElement" BasedOn="{StaticResource BaseStyle}">...</Style>
</ResourceDictionary>
我包括在App.xaml中这个文件,如下所示:
<Application.Resources>
<ResourceDictionary Source="GlobalStyles.xaml" />
</Application.Resources>
这应该工作,但我得到了下面的生成错误对于ExtendedStyle:Error: Index was outside the bounds of the array.
。如果我删除了BasedOn
指令,我得到了BaseStyle
的相同错误,但没有找到ExtendedStyle
。这真的很奇怪。任何人都可以解释这个错误吗?
编辑: 的风格是:
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<!-- Background -->
<Setter Property="Control.Background" Value="{StaticResource White0ColorBrush}"/>
<!-- Border -->
<Setter Property="Control.BorderBrush" Value="{StaticResource Green1ColorBrush}"/>
<Setter Property="Control.BorderThickness" Value="0"/>
<!-- Font -->
<Setter Property="Control.FontWeight" Value="Light"/>
<Setter Property="Control.Foreground" Value="{StaticResource Green0ColorBrush}"/>
</Style>
和
<Style x:Key="ExtendedStyle" TargetType="FrameworkElement" BasedOn="{StaticResource BaseStyle}">
<Setter Property="Control.Background" Value="{StaticResource Green1ColorBrush}"/>
<Setter Property="Control.Foreground" Value="{StaticResource White0ColorBrush}"/>
</Style>
我不觉得有什么特别之处这两种风格。重新启动VS后,我得到了另一种依赖于ExtendedStyle
的样式的错误。
编辑2: 我创建了一个新的空WPF应用程序项目,并列入GlobalStyles.xaml
文件中的app.xml中。那很好。然后我说只是这些行到window.xaml,并得到了同样的错误:
<Grid>
<Border BorderThickness="2" CornerRadius="5" BorderBrush="{StaticResource White0ColorBrush}" Style="{StaticResource ExtendedBaseStyle}">
<Grid>
<TextBox x:Name="SearchField" Height="20" BorderThickness="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" />
</Grid>
</Border>
</Grid>
在样本中试过,不会抛出任何异常。你尝试重新启动VS?另外,你可以发布XAML的样式吗? – sthotakura
疯狂猜测 - 也许尝试将'GlobalStyles.xaml'与app.xaml中的合并? –
Viv
@sthotakura重新启动VS没有帮助。我也添加了样式。 – 341008