无法从Array中的类访问List

时间:2018-03-18 03:23:47

标签: c# frameworks game-engine

我是一名初学者,如果能帮助我找出代码中存在的问题,我将非常感谢。代码特别令人困惑,主要是因为它来自框架。评论应该能够让我们理解。

// Create an IBindable List
public static List<IBindable> KyzerBindables = new List<IBindable>();

// Attach elements to a list, for better control over all of them
internal static void AttachBindablesToList(IReadOnlyList<Drawable> children)
{
    // For all the children classes located in Drawable list
    for (int i = 0; i < children.Count; i++) // children.Count returns 4
    {
        // For all of the SettingsSubsection which are present in the Drawable array
        for (int l = 0; l < (children[i] as SettingsSubsection).Children.Count; l++) // (children[i] as Subsection).Children.Count returns 0.
        {
            // Get a specific element
            var element = (children[i] as SettingsSubsection).Children[l];

            // if is a SettingsCheckbox
            if (element.GetType() == typeof(SettingsCheckbox))
                KyzerBindables.Add((element as SettingsCheckbox).Bindable);
        }
    }
}
// in another class
public class KyzerSection: SettingsSection
{
    public KyzerSection()
    {
        Children = new Drawable[]
        {
            new KyzerMiscellaneous(),
        };

        ...AttachElementsToList(Children);
    }
}

public class KyzerMiscellaneous: SettingsSubsection
{
    [BackgroundDependencyLoader] // Calls load, framework thing.
    private void load(OsuConfigManager config)
    {
        Children = new Drawable[]
        {
            new SettingsCheckbox
            {
                LabelText = "Something here",
                Bindable = new BindableBool(false),
            }
        };
    }
}

我的问题是,第二个for循环甚至没有为AttachBindablesToList启动。无论出于何种特殊原因,它都不会收到计数。我不确定我做错了什么。

修改

如果GitHub存储库问题以任何方式解决了一些问题,请随意浏览并检查包含这些更改的提交。 https://github.com/Frontear/osuKyzer/issues/3

1 个答案:

答案 0 :(得分:2)

在审核了你的github存储库后,我认为问题是由以下原因造成的:

private void load(params here)

在AttachBindablesToList时没有调用上面的内容。这导致空

(children[i] as SettingsSubsection).Children.Count

最好的选择是创建一个空的实例化方法

public KyzerMiscellaneous() { /* create Drawable elements */ }
// then
[BackgroundDependancyLoader]
private void load(params here) { /* doSomething */ }

这将允许访问子列表,因为它之前已经初始化,因此允许第二个循环正确运行,并将IBindables推送到列表中。