资源声明顺序改变时抛出的异常 [英] Exception thrown when resources declaration order changed
问题描述
按此顺序声明资源时出现异常:
I get exception when declare resources in this order:
<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>
当以这种方式声明时,一切正常:
and when declare this way, all works:
<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;component/window1.xaml'第 18 行第 37 行.
Cannot convert the value in attribute 'ItemsSource' to object of type 'System.Collections.IEnumerable'. 'System.Windows.Markup.ArrayExtension' is not a valid value for property 'ItemsSource'. Error at object 'System.Windows.Controls.ComboBox' in markup file 'WpfResourcesBug;component/window1.xaml' Line 18 Position 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>
推荐答案
我想你可能会找到一个可能的解决方案 这里
I think you might find a possible solution here
这篇关于资源声明顺序改变时抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!