从XAML引用类中定义的枚举 [英] Referencing enum defined in a class from within XAML

查看:74
本文介绍了从XAML引用类中定义的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Expression Blend 4(Silverlight项目)中,我有一个UserControl,向其中添加了CLR属性.此属性是枚举类型,在UC中定义.我已将ChangePropertyAction行为附加到UC实例.但是,XAML解析器会给出以下错误(以及其他错误):

In Expression Blend 4 (Silverlight project) I have a UserControl to which I have added a CLR property. This property is an enum type, which is defined within the UC. I have attached a ChangePropertyAction behaviour to an instance of the UC. However, the XAML parser gives the following error (among others):

'+'在名称中无效

'+' is not valid in a name

这是因为已生成以下XAML(代码段):

This is because the following XAML (snippet) has been generated:

<local:SomeControl Margin="155,113,317,0" d:LayoutOverrides="Width, Height">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
      <ei:ChangePropertyAction PropertyName="MyProp">
        <ei:ChangePropertyAction.Value>
          <local:SomeControl+MyEnum>Second</local:SomeControl+MyEnum> <----- Error on this line caused by the '+' 
        </ei:ChangePropertyAction.Value>
      </ei:ChangePropertyAction>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</local:SomeControl>

背后的代码:

public partial class SomeControl : UserControl
{
    public SomeControl()
    {
        // Required to initialize variables
        InitializeComponent();
    }

    public MyEnum MyProp
    {
        get; set;
    }

    public enum MyEnum
    {
        First,
        Second,
        Third
    }
}

一个简单的解决方法是从类(例如SomeControl_MyEnum)中促进"枚举,但是有没有更清洁的解决方案?

A simple workround is to "promote" the enum out from within the class (eg SomeControl_MyEnum), but is there a cleaner solution?

推荐答案

不支持在Xaml中使用嵌套类型名称.您仍然可以指定属性的值,而无需引用类型名称.以下任何一种方法都可以工作:

Using a nested type name in Xaml is not supported. You can still specify the value of the property without referring to the type name. Either of the following should work:

<local:SomeControl Margin="155,113,317,0" d:LayoutOverrides="Width, Height">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
      <ei:ChangePropertyAction PropertyName="MyProp">
        <ei:ChangePropertyAction.Value>Second</ei:ChangePropertyAction.Value>
      </ei:ChangePropertyAction>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</local:SomeControl>

<local:SomeControl Margin="155,113,317,0" d:LayoutOverrides="Width, Height">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
      <ei:ChangePropertyAction PropertyName="MyProp" Value="Second" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</local:SomeControl>

如果从Xaml引用MyEnum类型对您来说很重要,则需要将定义移出SomeControl类.

If it is important to you to be able to refer to the MyEnum type from Xaml, you will need to move the definition out of the SomeControl class.

这篇关于从XAML引用类中定义的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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