XAML 编译失败,但没有任何错误消息,如果用户定义的对象是第一个资源并且紧随其后的是 x:Array 资源
XAML fails to compile, but without any error message, if user-defined object is first resource and followed immediately by x:Array resource
我发现这个问题非常难运行ge,可能是 XAML/Visual Studio 错误。我希望其他人发现它更少 st运行ge 并且解释为什么我做的是错的,and/or 比仅仅以不同的顺序声明资源更好的解决方法。
我有这个简单的 XAML:
<Window x:Class="TestSOAskArrayXaml.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:TestSOAskArrayXaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:A x:Key="a1"/>
<x:Array x:Key="listBoxItems" Type="{x:Type system:Double}">
<system:Double>0.25</system:Double>
</x:Array>
</Window.Resources>
<Grid/>
</Window>
当我尝试编译项目时,出现以下错误:
1>...MainWindow.xaml.cs(25,13,25,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
我明白错误的含义,但不明白为什么会这样。 XAML 看起来很好,编译它没有错误,但由于某种原因,自动生成的 .g.i.cs 文件通常会放置 InitializeComponent()
没有被创建或使用(即甚至如果该文件存在于先前成功编译的文件中,则它仍未编译到程序集中。
如果我简单地颠倒资源的顺序,效果很好:
<Window.Resources>
<x:Array x:Key="listBoxItems" Type="{x:Type system:Double}">
<system:Double>0.25</system:Double>
</x:Array>
<local:A x:Key="a1"/>
</Window.Resources>
附加信息:
A
是我项目中的任何 class。出于本次测试的目的,它被声明为 class A { }
,即一个空的 class,但我首先 运行 解决了这个问题,将转换器实例放入资源中。
- 如果我使用内置类型而不是
A
,例如<system:String x:Key="a1">Some string</system:String>
,错误没有发生。
- 如果我将一个内置类型的对象作为资源放在用户定义类型
A
对象和我的数组资源对象之间,它也可以正常工作!
换句话说,似乎将一个或多个用户定义的类型化对象作为第一个资源元素,紧接着是一个数组对象,导致编译失败。其他组合似乎工作得很好。
有人可以解释为什么这是预期的行为(以及除了后方运行 ging 我的资源之外我应该做些什么来避免它),或者确认我认为这是一个错误并不完全疯狂XAML构建过程?
编辑:
考虑到这可能是一个真正的错误,我继续并在此处打开一个连接错误报告:https://connect.microsoft.com/VisualStudio/feedback/details/1441123/xaml-fails-to-compile-without-error-if-user-defined-object-is-first-resource-and-followed-immediately-by-x-array-resource
另见 related/similar 堆栈溢出问题:The name 'InitializeComponent' does not exist in the current context : strange behaviour
WPF 团队更新了 Connect 报告(参见问题中的 link),声明该错误的修复将在下一版本的 .NET 中发布:
Posted by Sachin [MSFT] on 3/9/2016 at 3:53 PM
The WPF team has reviewed this issue and fixed in next version of .NET. We thank you for the feedback and consider this issue resolved – WPF team
遗憾的是,Microsoft 已停用 Connect 站点,因此实际报告不再可用。但是我在这个问题中问到的问题应该不会再出现了。
我发现这个问题非常难运行ge,可能是 XAML/Visual Studio 错误。我希望其他人发现它更少 st运行ge 并且解释为什么我做的是错的,and/or 比仅仅以不同的顺序声明资源更好的解决方法。
我有这个简单的 XAML:
<Window x:Class="TestSOAskArrayXaml.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:TestSOAskArrayXaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:A x:Key="a1"/>
<x:Array x:Key="listBoxItems" Type="{x:Type system:Double}">
<system:Double>0.25</system:Double>
</x:Array>
</Window.Resources>
<Grid/>
</Window>
当我尝试编译项目时,出现以下错误:
1>...MainWindow.xaml.cs(25,13,25,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
我明白错误的含义,但不明白为什么会这样。 XAML 看起来很好,编译它没有错误,但由于某种原因,自动生成的 .g.i.cs 文件通常会放置 InitializeComponent()
没有被创建或使用(即甚至如果该文件存在于先前成功编译的文件中,则它仍未编译到程序集中。
如果我简单地颠倒资源的顺序,效果很好:
<Window.Resources>
<x:Array x:Key="listBoxItems" Type="{x:Type system:Double}">
<system:Double>0.25</system:Double>
</x:Array>
<local:A x:Key="a1"/>
</Window.Resources>
附加信息:
A
是我项目中的任何 class。出于本次测试的目的,它被声明为class A { }
,即一个空的 class,但我首先 运行 解决了这个问题,将转换器实例放入资源中。- 如果我使用内置类型而不是
A
,例如<system:String x:Key="a1">Some string</system:String>
,错误没有发生。 - 如果我将一个内置类型的对象作为资源放在用户定义类型
A
对象和我的数组资源对象之间,它也可以正常工作!
换句话说,似乎将一个或多个用户定义的类型化对象作为第一个资源元素,紧接着是一个数组对象,导致编译失败。其他组合似乎工作得很好。
有人可以解释为什么这是预期的行为(以及除了后方运行 ging 我的资源之外我应该做些什么来避免它),或者确认我认为这是一个错误并不完全疯狂XAML构建过程?
编辑:
考虑到这可能是一个真正的错误,我继续并在此处打开一个连接错误报告:https://connect.microsoft.com/VisualStudio/feedback/details/1441123/xaml-fails-to-compile-without-error-if-user-defined-object-is-first-resource-and-followed-immediately-by-x-array-resource
另见 related/similar 堆栈溢出问题:The name 'InitializeComponent' does not exist in the current context : strange behaviour
WPF 团队更新了 Connect 报告(参见问题中的 link),声明该错误的修复将在下一版本的 .NET 中发布:
Posted by Sachin [MSFT] on 3/9/2016 at 3:53 PM
The WPF team has reviewed this issue and fixed in next version of .NET. We thank you for the feedback and consider this issue resolved – WPF team
遗憾的是,Microsoft 已停用 Connect 站点,因此实际报告不再可用。但是我在这个问题中问到的问题应该不会再出现了。