在XAML中初始化ICollection [英] Initializing an ICollection within XAML

查看:85
本文介绍了在XAML中初始化ICollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图做这样的事情...

Trying to do something like this...

public class MiniObject
{
    public bool Checked { get; set; }
    public String Caption { get; set; }

    public ICollection<MiniObject> Content { get; set;}
}


<Window.Resources>
    <local:MiniObject x:Key="RootItem" Caption="Item0" Checked="True">
        <local:MiniObject.Content>
            <local:MiniObject Caption="Item1" Checked="True">
            </local:MiniObject>
            <local:MiniObject Caption="Item2" Checked="True">
            </local:MiniObject>
        </local:MiniObject.Content>
    </local:MiniObject>
</Window.Resources>

返回错误当然不起作用:

This of course doesn't work returning the error:

Object of type 'WorkflowTest.MiniObject' cannot be converted to type 'System.Collections.Generic.ICollection`1[WorkflowTest.MiniObject]'.

是否可以在WPF中执行此操作?如果是这样,我根本不需要更改对象的形状,或者我可以简单地提供仅WPF使用的专用对象,例如...

Is there a way to do this within WPF? If so do I need to change the shape of my objects at all or can I simply provide a specialized object that only WPF uses like...

<Window.Resources>
    <local:MiniObject x:Key="RootItem" Caption="Item0" Checked="True">
        <local:MiniObject.Content>
            <local:MiniObjectCollection>
                <local:MiniObject Caption="Item1" Checked="True">
                </local:MiniObject>
                <local:MiniObject Caption="Item2" Checked="True">
                </local:MiniObject>
            </local:MiniObjectCollection>
        </local:MiniObject.Content>
    </local:MiniObject>
</Window.Resources>


推荐答案

您正在尝试使用XAML的隐式集合语法。为此,属性(在本例中为 Content )必须具有实现 ICollection 的类型。注意:不是 ICollection ,而是实现 ICollection 的类型。

You're trying to use XAML's implicit collection syntax. In order to do this, the property (Content, in this case) must be of a type that implements ICollection. Note: not ICollection, but a type that implements ICollection.

您不能只使用接口,因为 XamlReader 需要知道要创建哪种类型的对象。如果您尚未告知类型,应该如何确定?通过搜索程序集可用的所有类型,找到实现 ICollection< MiniObject> 的类型,舍弃那些没有无参数构造函数的类型,然后选择一个随机?否

You can't just use an interface because the XamlReader needs to know what type of object to create. If you haven't told it the type, how should it decide? By searching through all of the types available to your assembly, finding the ones that implement ICollection<MiniObject>, discarding the ones that don't have a parameterless constructor, and then choosing one at random? No.

当您将 Content 定义为 List< MiniObject> XamlReader 知道应创建哪种类型的对象。由于该类型实现了 ICollection ,因此可以使用隐式集合语法。因此,它只是创建对象并调用 Add 添加子项,如果您将子项卡在了 MiniObject 会出现运行时错误。

When you define Content as List<MiniObject>, the XamlReader knows what type of object it should create. Since that's a type that implements ICollection, it can use the implicit collection syntax. So it just creates the object and calls Add to add the child items, and if you stuck a child item in there that isn't a MiniObject you'll get a runtime error.

您在中说我需要避免使用实际实现内容属性。在这种情况下,您将无法使用隐式集合语法。您将需要做第二个示例中的操作:显式定义一个实现 ICollection< MiniObject> 的类型,并在XAML中添加一个子元素以显式创建它。

You say that "I need to avoid using an actual implementation" in your Content property. In that case, you cannot use the implicit collection syntax. You will need to do what you do in your second example: explicitly define a type that implements ICollection<MiniObject>, and add a child element in your XAML to create it explicitly.

这篇关于在XAML中初始化ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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