【问题标题】:Bind subarray in listview xamarin mvvm在listview xamarin mvvm中绑定子数组
【发布时间】:2018-01-04 16:06:21
【问题描述】:

试图在列表视图中绑定一个数据子数组。 假设我有一个这种格式的 json:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

假设这是我的模型

 public class GlossDef
{
    public string para { get; set; }
    public List<string> GlossSeeAlso { get; set; }
}

public class GlossEntry
{
    public string ID { get; set; }
    public string SortAs { get; set; }
    public string GlossTerm { get; set; }
    public string Acronym { get; set; }
    public string Abbrev { get; set; }
    // ===edit=== lets assume I have GlossDef is a list.
    public List<GlossDef> GlossDef { get; set; }
    public string GlossSee { get; set; }
}

public class GlossList
{
    public GlossEntry GlossEntry { get; set; }
}

public class GlossDiv
{
    public string title { get; set; }
    public GlossList GlossList { get; set; }
}

public class Glossary
{
    public string title { get; set; }
    public GlossDiv GlossDiv { get; set; }
}

public class RootObject
{
    public Glossary glossary { get; set; }
}

让我们说这是我的 xaml:

<ListView x:Name="listview" SeparatorVisibility="None" 
              HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="20, 10">
                        <Label  Text="{Binding Title}"/> //this works fine
                        <Label  Text="{Binding Para}"/> //how can I do this
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

有什么方法可以在列表视图中绑定子数组吗?我尝试使用嵌套列表视图,但我的应用程序刹车,我读到嵌套列表视图通常会使我的应用程序崩溃。我正在调查this thread,这正是我的问题,他也在 SO (How To Bind the JSON Sub Array Data into Listview in xamarin.forms) 中发布,但没有收到正确的答案。任何帮助将不胜感激。谢谢你:))

【问题讨论】:

    标签: c# json xamarin data-binding xamarin.forms


    【解决方案1】:

    在您的 ListView 中,您只能使用 List 或 ObservableCollection 对象。您的列表在 List&lt;string&gt; GlossSeeAlso 中,因此您应该将此对象绑定到您的 ListView 并使用“{Binding .}”来可视化

    GML

    XML

    ...

    【讨论】:

    • 请检查我的编辑。这样我怎么绑定para?!或者有没有其他方法可以做到这一点? @alessandro-caliaro 。该服务还返回所有 json(词汇表)。那么listview的itemsource应该是什么呢?
    • 您必须将 GlossDef 绑定到您的 ListView 并将“para”属性绑定到您的控件
    【解决方案2】:

    这看起来像一个分组列表视图。

    您可能必须将数据处理成更适合这样的分组列表视图的格式。

    public class EntryList : List<GlossEntry>
    {
        public string Title { get; set; }
    
        public List<GlossEntry> Entries {get; set;}
    }
    

    See here for more details

    【讨论】:

      猜你喜欢
      • 2018-08-14
      • 2021-07-21
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 2021-11-08
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多