[英]Cannot access List from a class within an Array
我是一名初学者开发人员,如果您能帮助我找出代码中的问题,将不胜感激。 该代码特别令人困惑,主要是因为它源自框架。 这些评论应该能够使我们理解。
// 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
在查看了您的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推送到您的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.