将列表传递到RootElement c#中的Section

本文关键字:中的 Section RootElement 列表 | 更新日期: 2023-09-27 18:29:29

我正在向RootElement的一部分添加某些值,如下所示:-

NavigationRoot = new RootElement("Menu"){ //Here we create the root of the elements
     new Section("Pages"){
      new StringElement ("Feed"),
      new StringElement ("Messages"),
      new StringElement ("Nearby"),
      new StringElement ("Events"),
      new StringElement ("Friends"),
     },

现在,StringElements是硬编码的。我想将从API调用中获取的字符串列表传递给部分。这个列表将是动态的。如何传递此列表以创建列表中具有字符串值的项目部分?

感谢您的帮助。

将列表传递到RootElement c#中的Section

如果你有一个字符串数组,你可以使用Linq来尝试这个:-

from page in myStringArray select new StringElement (page) as Element; 
var section = new Section("Pages");
foreach(var s in MyListOfStrings) {
  section.Add(new StringElement(s));
}