【发布时间】:2009-03-31 23:48:20
【问题描述】:
按此顺序声明资源时出现异常:
<Window.Resources>
<sys:Object x:Key="resourceA"></sys:Object>
<x:Array x:Key="resourceB" Type="sys:String">
<sys:String>foo</sys:String>
</x:Array>
</Window.Resources>
当以这种方式声明时,一切正常:
<Window.Resources>
<x:Array x:Key="resourceB" Type="sys:String">
<sys:String>foo</sys:String>
</x:Array>
<sys:Object x:Key="resourceA"></sys:Object>
</Window.Resources>
抛出的异常是:
无法转换属性中的值 'ItemsSource' 到对象类型 'System.Collections.IEnumerable'。 'System.Windows.Markup.ArrayExtension' 不是属性的有效值 '项目来源'。对象错误 'System.Windows.Controls.ComboBox' 在 标记文件 'WpfResourcesBug;组件/window1.xaml' 第 18 行位置 37。
完整代码:
<Window x:Class="WpfResourcesBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<sys:Object x:Key="resourceA"></sys:Object>
<x:Array x:Key="resourceB" Type="sys:String">
<sys:String>foo</sys:String>
</x:Array>
</Window.Resources>
<StackPanel>
<ComboBox SelectedIndex="0" ItemsSource="{StaticResource resourceB}" />
</StackPanel>
</Window>
【问题讨论】: