WindowsPhone 8:将 json 数据添加到列表视图.堆栈面板内的模板,无需使用模态

本文关键字:模态 堆栈 json 数据 添加 视图 列表 WindowsPhone | 更新日期: 2023-09-27 17:56:30

我想通过使用 C# 代码将 json 数据添加到列表视图项内的堆栈面板(名称:展开)中,而无需使用模态...我已经解析了 json 数据...但我不知道如何在不使用模式的情况下将其附加到名为"扩展"的堆栈面板中的列表视图项目这是我的 XAML

    <ListView.Items>
                </ListView.Items>                
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                      <Border Tapped="Border_Tap" Height="90">
                         <StackPanel  x:Name="ParentStackPanel" Width="380" Margin="0,5,0,5">

                                <StackPanel Background="Black" Margin="60,-80,0,0" Grid.Row="0" Grid.Column="1"   VerticalAlignment="Center" HorizontalAlignment="Left">
                                    <TextBlock Text="{Binding CompanyName1}" FontSize="20" FontStyle="Normal">
                                    </TextBlock>
                                </StackPanel> 
<StackPanel DataContext="expand1"  Name="expand" Background="White" Height="220">

                                </StackPanel>
                            </Border>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我的 JSON 在下面给出

我想在不使用模式的情况下将每个发票 json 对象附加到列表视图项目堆栈面板(名称 = "展开")

  {
  "Example": [
    {
      "Invoice": {
        "TF": " 200",
        "BF": " 200",
        "MD": "10",
        "EFM": " 12",
        "ATT": "4"
      },
      "TF": "200"
    },
    {
      "Invoice": {
        "TF": " 49",
        "BF": " 49",
        "MD": "4",
        "EFM": " 14",
        "ATT": "4"
      },
      "TF": "49"
    },
    {
      "Invoice": {
        "TF": " 49",
        "BF": " 49",
        "MD": "3",
        "FPK": " 16",
        "ATT": "4"
      },
      "TF": "49"
    },
    {
      "Invoice": {
        "TF": " 49",
        "BF": " 49",
        "MD": "2",
        "FPK": " 12",
        "ATT": "1"
      },
      "TF": "49"
    },
    {
      "Invoice": {
        "TF": " 90",
        "BF": " 90",
        "MD": "4",
        "FPK": " 12",
        "ATT": "4"
      },
      "TF": "90"
    },
    {
      "Invoice": {
        "TF": " 49",
        "BF": " 49",
        "MD": "3",
        "FPK": " 16",
        "ATT": "4"
      },
      "TF": "49"
    }
  ]
}

WindowsPhone 8:将 json 数据添加到列表视图.堆栈面板内的模板,无需使用模态

感谢 http://json2csharp.com/为您的 JSON 获取类非常方便:

public class Invoice
{
     public string TF { get; set; }
     public string BF { get; set; }
     public string MD { get; set; }
     public string EFM { get; set; }
     public string ATT { get; set; }
     public string FPK { get; set; }
 }
 public class Example
 {
     public Invoice Invoice { get; set; }
     public string TF { get; set; }
 }
 public class RootObject
 {
     public List<Example> Example { get; set; }
 }

然后从 NuGet 下载 Json.NET http://www.newtonsoft.com/json 并反序列化 json:

var objects = JsonConvert.DeserializeObject<RootObject>(jsonString);

最后,将 listView.ItemSource 属性设置为等于 objects,并确保使用上述类中指示的相同属性名称。如果要使用其他名称,请尝试如下操作:

例:

public class Invoice
{
     [Newtonsoft.Json.JsonProperty(PropertyName = "CompanyName1")]
     public string TF { get; set; }
     [Newtonsoft.Json.JsonProperty(PropertyName = "ComapnyName2")]
     public string BF { get; set; }
 }

让我知道它是否有效!