I am attempting to use XAML to represent and load a graph of custom types that form a semantic model for a business domain.
我试图使用XAML来表示和加载自定义类型的图形,这些类型构成了业务域的语义模型。
One thing I would like to do is to be able to obtain a reference to the "root object" of the graph from an object deeper in the tree. I cannot find any straightforward way of doing this other than resorting to barbarous practices like having the root object as a singleton.
我想做的一件事是能够从树中更深的对象获得对图的“根对象”的引用。除了采用野蛮的做法,例如将根对象作为单身人士之外,我找不到任何直接的做法。
Any ideas?
1 个解决方案
#1
I don't know that this is a problem unique to XAML. All XAML is doing is instantiating your "child" objects and adding them to a collection property on the "parent" object. So if you had something like:
我不知道这是XAML独有的问题。所有XAML都在实例化你的“子”对象并将它们添加到“父”对象的集合属性中。所以,如果你有类似的东西:
<my:Category Name="Products">
<my:Category.Subcategories>
<my:Category Name="Clothing" />
<my:Category Name="Jewellery" />
</my:Category.Subcategories>
</my:Category>
... then you'd end up with a "Products" category with two subcategories. It'd be up to you to code up your "Category" class in such a way that adding a subcategory saves a reference to the "owner" of that subcategory somewhere. I'd probably use a custom collection type (override the Add and Remove methods so that they set the "Parent" or "Owner" property on the object being added).
...那么你最终会得到一个带有两个子类别的“产品”类别。您应该以这样的方式编写“类别”类,即添加子类别可以在某处保存对该子类别的“所有者”的引用。我可能会使用自定义集合类型(覆盖Add和Remove方法,以便它们在要添加的对象上设置“Parent”或“Owner”属性)。
#1
I don't know that this is a problem unique to XAML. All XAML is doing is instantiating your "child" objects and adding them to a collection property on the "parent" object. So if you had something like:
我不知道这是XAML独有的问题。所有XAML都在实例化你的“子”对象并将它们添加到“父”对象的集合属性中。所以,如果你有类似的东西:
<my:Category Name="Products">
<my:Category.Subcategories>
<my:Category Name="Clothing" />
<my:Category Name="Jewellery" />
</my:Category.Subcategories>
</my:Category>
... then you'd end up with a "Products" category with two subcategories. It'd be up to you to code up your "Category" class in such a way that adding a subcategory saves a reference to the "owner" of that subcategory somewhere. I'd probably use a custom collection type (override the Add and Remove methods so that they set the "Parent" or "Owner" property on the object being added).
...那么你最终会得到一个带有两个子类别的“产品”类别。您应该以这样的方式编写“类别”类,即添加子类别可以在某处保存对该子类别的“所有者”的引用。我可能会使用自定义集合类型(覆盖Add和Remove方法,以便它们在要添加的对象上设置“Parent”或“Owner”属性)。