【问题标题】:Cannot access List from a class within an Array无法从数组中的类访问列表
【发布时间】:2018-03-18 03:23:46
【问题描述】:

我是一名初学者,如果您能帮助我找出代码中的问题,我将不胜感激。代码特别令人困惑,主要是因为它源自框架。 cmets应该可以让我们有点理解。

// 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

【问题讨论】:

  • 我很抱歉。我不小心发布了。
  • 这里并不神秘。如果children.Count 为0,则列表中没有元素。使用断点来帮助更好地确定发生了什么
  • 我用更多信息更新了我的问题。
  • 你有没有单步调试过代码?
  • 是的,多次。如果 GitHub 存储库问题以任何方式可以解决一些问题,请随时导航到那里并检查包含这些更改的提交。 github.com/Frontear/osuKyzer/issues/3

标签: c# frameworks game-engine


【解决方案1】:

查看您的 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 推送到您的列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多