资源字典中的超出错误

时间:2013-06-18 12:56:29

标签: wpf styles

我已将我的共享样式放在我包含在应用程序中的单个资源字典中。例如,在名为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应用程序项目,并在app.xml中包含了GlobalStyles.xaml文件。那很好。然后我将这些行添加到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>

1 个答案:

答案 0 :(得分:0)

它必须是我正在使用的VS2012的错误。我搬到了VS2010,问题就消失了。我和VS2012设计师有很多问题。它经常挂起,抛出异常,抱怨语法错误,当没有,现在这个。我现在会坚持VS2010。