奇怪的错误WPF附加属性格式 [英] Weird WPF error with attached propery

查看:114
本文介绍了奇怪的错误WPF附加属性格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到奇怪的行为。我有一个强烈的感觉,这是.NET工具链中的错误。

I see strange behavior. I have a strong feeling that it is a bug in .net toolchain.

重现步骤:

  1. 创建WPF应用程序。
  2. 在创建解决方案的类库。
  3. 在类库定义一些公共类(可以为空)。
  4. 定义某个类的WPF应用程序与附加属性和窗口为目标类。
  5. 它附加到窗口,并提供价值

和你得到的错误:

对象窗口已经有了   孩子,可以不加''。 窗口即可   只接受一个孩子。

The object 'Window' already has a child and cannot add ''. 'Window' can accept only one child.

如果你改变结构,使类附加属性和价值类相同的组件(可以是类库或WPF应用程序),它工作正常。

If you change structure so that class with attached property and value class in same assembly (can be either Class Library or Wpf Application) it works fine.

另外,如果你把code含量后,工作正常。这给了错误:

Also it works fine if you put the code after content. This gives error:

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"
        xmlns:my2="clr-namespace:ClassLibrary1"
        Title="MainWindow" Height="350" Width="525">

    <my2:Property.MyProperty>
        <my:ValueClass />
    </my2:Property.MyProperty>

    <Grid>

    </Grid>
</Window>

这不:

<Grid>

</Grid>    

<my2:Property.MyProperty>
    <my:ValueClass />
</my2:Property.MyProperty>

任何想法?

谢谢!

推荐答案

这是由自我指涉的方式,XAML编译。虽然你有一个命名空间引用包含附加属性的组件,应用程序组件,这很集会其实是在被编译在您需要的XAML编译器来决定该元素语法是指附加属性的时间进程,不是一个普通的元素。

This is caused by the self-referential way that XAML is compiled. Although you have a namespace reference to the assembly containing the attached property, the application assembly, that very assembly is in fact in the process of being compiled at the time you need the XAML compiler to decide that the element syntax refers to an attached property and not an ordinary element.

正如您所发现的,把附加属性的内容后,就足够了一丝得到它做出正确的选择,即使不完整的信息。在另一方面,知道这就是问题所在,你也可以简单地把附加属性到外部组件,例如到控制库,而这也将避免鸡和蛋的问题。

As you have discovered, putting the attached property after the content is enough of a hint to get it to make the right choice even with incomplete information. On the other hand, knowing that this is the problem, you can also simply put the attached property into an external assembly, for example into the control library, and this will also avoid the chicken-and-the-egg problem.

在一个完美的世界C#编译器和XAML编译器将完全相互整合,这个问题会自行消失但在那之前,我们将不得不使用的解决方法。

In a perfect world the C# compiler and the XAML compiler would be fully integrated with each other and this problem would go away but until then we'll have to use workarounds.

编辑:

另一个试图解释这一点,因为它是一个有点复杂。

Another try to explain this because it is a little complicated.

在编译应用程序组件,它包含了C#和XAML。在示例code以上的XAML是指在同一个组件的C#code定义的附加属性。因此,XAML编译器需要让自己的应用程序集的编译版本,以便知道 Property.MyProperty 是附加属性。但是C#编译器不能编译应用程序组件,但因为XAML没有被编译。这是鸡和蛋的问题:在C#和XAML都依赖于对方

When you compile your application assembly, it contains both C# and XAML. In the sample code above the XAML refers to an attached property defined in the C# code of the same assembly. So the XAML compiler needs to have a compiled version of your application assembly in order to know that Property.MyProperty is an attached property. But the C# compiler can't compile your application assembly yet because the XAML hasn't been compiled. This is the chicken-and-the-egg problem: the C# and XAML both depend on each other.

有东西得给和XAML编译器向前走,并试图用不完整的信息编译应用程序的XAML文件,即使应用程序组件没有被编译呢。在这样做的过程中它可以让像没有检测到在同一个组件定义附加属性的错误。为什么它有时工作?我们可以注意到,他们已经走了一些努力来处理一些常见的情况,因此确实出现了工作的大部分时间,但尽管如此,它不为你所发现的时候所有的工作。

Something has got to give and the XAML compiler goes ahead and tries to compile your application's XAML files with incomplete information, even though the application assembly hasn't been compiled yet. In the course of doing this it can make mistakes like not detecting attached properties defined in the same assembly. Why does it work sometimes? We can note that they have gone to some effort to handle some common cases so it does appear to work most of the time but nevertheless it does not work all of the time as you have discovered.

最简单的解决方案是你的附加属性转移到外部程序集。像控制附加的属性通常与图书馆相关反正所以这个问题通常不会出现。

The easiest solution is to move your attached property into an external assembly. Attached properties like controls are generally associated with libraries anyway so the problem doesn't usually arise.

这篇关于奇怪的错误WPF附加属性格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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