有没有办法在 Silverlight XAML 中实例化“类型"? [英] Is there any way to instantiate a 'Type' in Silverlight XAML?

查看:33
本文介绍了有没有办法在 Silverlight XAML 中实例化“类型"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,Silverlight 缺乏非常引人注目的 x:TypeMarkupExtension(MarkupExtension 不支持Silverlight).是否有任何动态解决方法?

It's well known that Silverlight lacks the very compelling x:Type MarkupExtension (MarkupExtension is not supported in Silverlight at all). Is there any dynamic workaround for it?

枚举怎么样 (x:Static)?

What about enums (x:Static)?

我需要将 CommandParameter 设置为 TypeEnum 值,Silverlight 不支持这些值!

My need is to have a CommandParameter set to a Type or Enum value, neither of these are supported in Silverlight!

推荐答案

这通常必须在代码隐藏中完成.即使您构建了一个公开 Type 类型属性的自定义对象,当通过 XAML 设置时,它也不会被正确转换".

This generally has to be done in the code-behind. Even if you build a custom object that exposes a property of type Type, it will not get properly "converted" when set via XAML.

这个额外的限制会阻止你创建像自定义 EnumValueProvider 这样的东西,它公开一个 Type 属性并用来自指定枚举类型的枚举值更新一个 Values 属性.拥有这样的类将允许您将 ComboBox 或 ListBox 绑定到完全在 XAML 中的枚举值列表.

This extra limitation prevents you from creating things like a custom EnumValueProvider, which exposes a Type property and updates a Values properties with the enum values from the specified enumeration type. Having a class like this would allow you to bind a ComboBox or ListBox to a list of enumeration values completely in XAML.

您可以创建一个 自定义 TypeConverter 对于上述 Type 属性,可以稍微解决此问题.但是您没有得到您期望的 xmlns 分辨率.但根据您的情况,这可能已经足够了.

You can create a custom TypeConverter for the above Type property, to somewhat work around this issue. But you don't get the xmlns resolution that you'd expect. But depending on your situation, this may be good enough.

这里是 另一个示例,它通过属性公开已知"类型,然后您可以绑定到这些类型.

Here is another example, that exposes "known" types via properties, which you can then bind to.

这篇关于有没有办法在 Silverlight XAML 中实例化“类型"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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