是否有元素属性的语法和atrribute属性语法之间的语义差别? [英] Is there any semantical difference between element property syntax and atrribute property syntax?

查看:107
本文介绍了是否有元素属性的语法和atrribute属性语法之间的语义差别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得元素属性语法和属性的属性的语法没有大的语义差别。然而,我发现,必须有一定的差异。

I thought that element property syntax and attribute property syntax have no big semantical difference. However, I found that there must be some difference.

例如。下面的例子只是演示了一个简单的触发:

E.g. The following example just demonstrates a simple trigger:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button><Button.Template>
  <ControlTemplate TargetType="{x:Type Button}">
    <TextBlock x:Name="hello" Text="Hello" />
    <ControlTemplate.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="Red" TargetName="hello"/>
     </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Button.Template></Button>
</Page>

不过,如果我用一个元素属性的语法触发财产属性,它抛出一个异常说二传手! (未触发)既需要的属性和值。

However, if I used a element property syntax for property Property of trigger, it throws an exception saying that setter! (not trigger) requires both property and value.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button><Button.Template>
  <ControlTemplate TargetType="{x:Type Button}">
    <TextBlock x:Name="hello" Text="Hello" />
    <ControlTemplate.Triggers>
      <Trigger Value="True">
        <Trigger.Property>IsMouseOver</Trigger.Property>
        <Setter Property="Foreground" Value="Red" TargetName="hello"/>
     </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Button.Template></Button>
</Page>

那么,什么是的隐藏的元素属性的语法和属性的属性语法之间的区别?

So, what is the hidden difference between element property syntax and attribute property syntax?

推荐答案

不应该有差别。我觉得你刚才发现的XAML分析器的错误。

There shouldn't be a difference. I think you just found a bug in the XAML parser.

该框架有特殊处理的二传手,触发和条件。请查看 Trigger.ReceiveTypeConverter 与反射器,其覆盖的属性setter的价值和产权属性的处理。我觉得是这样的话,它可以解析Value属性与基于产权性质上不同的类型。例如,解析红色为画笔,而不是只是一个字符串时,看到该物业前景和前景类型的画笔。

The framework has special handling for Setter, Trigger, and Condition. Check out Trigger.ReceiveTypeConverter with Reflector, which overrides the handling of property setters for the Value and Property properties. I think this is so that it can parse the Value property with a different type based on the Property property. For example, it parses "Red" as a Brush rather than just a String when it sees the Property is Foreground and that Foreground is of type Brush.

它看起来像那个勾覆盖名为触发器中值或属性的所有属性集,但它不能正确处理元素属性的语法。要看到效果,尝试创建一个标记扩展是这样的:

It looks like that hook overrides all sets of properties named Value or Property within a Trigger, but that it doesn't correctly handle the element property syntax. To see the effect, try creating a markup extension like this:

public class Test
    : MarkupExtension
{
    public DependencyProperty Property { get; set; }
    public DependencyProperty Property2 { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Property ?? Property2;
    }
}

下面的XAML将得到同样的错误作为你的第二个例子,你可以通过设置一个断点物业从未设置验证:

The following XAML will get the same error as your second example, and you can verify by setting a breakpoint that Property is never set:

<Trigger.Property>
    <local:Test>
        <local:Test.Property>IsMouseOver</local:Test.Property>
    </local:Test>
</Trigger.Property>

不过,这会工作,因为属性未命名属性:

However, this will work, because the property is not named "Property":

<Trigger.Property>
    <local:Test>
        <local:Test.Property2>IsMouseOver</local:Test.Property2>
    </local:Test>
</Trigger.Property>

和本会的工作,因为它使用属性语法:

And this will work, because it uses the attribute syntax:

<Trigger.Property>
    <local:Test Property="IsMouseOver"/>
</Trigger.Property>

如果你确实需要使用元素属性的语法,这会给你一个解决方法:创建的MarkupExtension具有类型命名的DependencyProperty比财产其他东西属性,并返回在ProvideValue。

If you really need to use the element property syntax, that will give you a workaround: create a MarkupExtension that has a Property of type DependencyProperty named something other than "Property", and return that in ProvideValue.

这篇关于是否有元素属性的语法和atrribute属性语法之间的语义差别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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