簡體   English   中英

在Listview Xamarin MVMVM中綁定子數組

[英]Bind subarray in listview xamarin mvvm

試圖在列表視圖中綁定數據的子數組。 可以說我有以下格式的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>

有什么辦法可以綁定列表視圖中的子數組嗎? 我嘗試使用嵌套列表視圖,但是我的應用程序剎車,並且我讀到嵌套列表視圖通常會使我的應用程序崩潰。 我一直在研究這個線程 ,這正是他在SO中發布的問題( 如何在xamarin.forms中將JSON子數組數據綁定到Listview中 ),但沒有得到正確的答案。 任何幫助將不勝感激。 謝謝 :))

在ListView中,您只能使用List或ObservableCollection對象。 您在List<string> GlossSeeAlso中的List<string> GlossSeeAlso因此也應將此對象綁定到ListView並使用“ {Binding。}”進行可視化

GML

XML格式

...

這看起來像一個分組列表視圖。

您可能需要將數據處理為更適合分組列表視圖的格式,如下所示。

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

    public List<GlossEntry> Entries {get; set;}
}

看到這里更多細節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM