如果用户定义的对象是第一个资源,并且紧随其后的是x:Array资源,则XAML编译失败,但没有任何错误消息 [英] XAML fails to compile, but without any error message, if user-defined object is first resource and followed immediately by x:Array resource

查看:72
本文介绍了如果用户定义的对象是第一个资源,并且紧随其后的是x:Array资源,则XAML编译失败,但没有任何错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现此问题非常奇怪,可能是XAML / Visual Studio错误。我希望其他人觉得它不那么奇怪,并且能解释我为什么做错了,和/或比仅以不同顺序声明资源更好的解决方法。

I find this issue very strange, possibly a XAML/Visual Studio bug. I am hoping someone else finds it less strange and has an explanation for why what I'm doing is wrong, and/or a better work-around than just declaring the resources in a different order.

我有这个简单的XAML:

I have this simple 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>

当我尝试编译项目时,出现以下错误:

When I attempt to compile the project, I get the following error:


1> ... MainWindow.xaml.cs(25,13,​​25,32):错误CS0103:名称'InitializeComponent'在当前上下文中不存在

1>...MainWindow.xaml.cs(25,13,25,32): error CS0103: The name 'InitializeComponent' does not exist in the current context

我了解错误的含义,但不了解为什么会发生。 XAML看起来不错,没有错误编译,但是由于某种原因,通常不会放置通常自动放置 InitializeComponent()的自动生成的.gics文件,或者使用(即,即使文件来自先前的成功编译,也没有被编译到程序集中)。

I understand the error's meaning, but not why it happens. The XAML seems fine, there are no errors compiling it, but for some reason the auto-generated .g.i.cs file where InitializeComponent() would normally be put is not being created or used (i.e. even if the file is there from a previous successful compilation, it's still not compiled into the assembly).

如果我只是反转资源的顺序,它将工作正常:

If I simply reverse the order of the resources, it works fine:

  <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 A {} ,即一个空类,但我首先遇到了将转换器实例放入资源的问题。

  • 如果我使用内置类型而不是 A ,例如< system:String x:Key = a1>某些字符串< / system:String> ,不会发生错误。

  • 如果我将内置类型的对象作为资源放置在用户定义的类型 A 对象和数组资源对象之间,则它也会可以正常工作

  • A is any class in my project. For the purposes of this test, it was declared as class A { }, i.e. an empty class, but I first ran into this problem putting converter instances into the resources.
  • If I use a built-in type instead of A, e.g. <system:String x:Key="a1">Some string</system:String>, the error does not happen.
  • If I place an object of a built-in type as a resource in between the user-defined type A object and my array resource object, it also works fine!

换句话说,似乎第一个对象是一个或多个用户定义类型的对象资源元素,紧随其后的是数组对象,将导致编译失败。其他组合似乎也很好用。

In other words, it seems as though having one or more user-defined typed objects as the first resource elements, followed immediately by an array object, causes compilation to fail. Other combinations seem to work just fine.



有人可以解释为什么这是预期的行为(以及我应该怎么做以避免)除了重新整理我的资源之外),还是确认我并不是完全以为这是XAML构建过程中的错误?


Can someone please explain either why this is expected behavior (and what should I be doing to avoid it besides just rearranging my resources), or confirm that I'm not entirely crazy in thinking this is a bug in the XAML build process?



编辑:

鉴于这可能是实际的错误,我继续在此处打开了一个连接错误报告: https://connect.microsoft.com/VisualStudio/feedback/details/1441123/xaml-fails-to-compile-without-error-if-user定义对象是第一个资源,紧接着是x数组资源

Given the likelihood of this being an actual bug, I went ahead and opened a Connect bug report here: 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

另请参见相关/相似的堆栈溢出问题:在当前上下文中不存在名称 InitializeComponent:奇怪的行为

See also related/similar Stack Overflow question: The name 'InitializeComponent' does not exist in the current context : strange behaviour

推荐答案

WPF团队更新了连接报告(请参阅相关链接),指出该错误的修复程序将在下一版.NET中发布:

The WPF team has updated the Connect report (see link in question), stating that the fix for the bug will be released in the next version of .NET:


Sachin [MSFT]发布于2016/3/9下午3:53


WPF团队已审核了此问题,并在下一版.NET中进行了修复。我们感谢您的反馈,并认为此问题已解决– WPF团队

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网站,因此实际报告不再被提供。但是我在这个问题中提出的问题应该不再发生。

Unfortunately, Microsoft has retired the Connect site, so the actual report is no longer available. But the problem I've asked about in this question should no longer occur.

这篇关于如果用户定义的对象是第一个资源,并且紧随其后的是x:Array资源,则XAML编译失败,但没有任何错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆