我是Windows手机开发的新手。我遇到了这个问题。我想通过使用绑定 Json 数据在网格内动态创建 textblock 。 我的Json数据是:
[ {"scheme":"SCHEME A","units":1,"amount":2000,"currency":"AED","approved_date":"2014-12-19","lockState":"Locked","id":7497,"withdrawal_request":0}, {"scheme":"SCHEME A","units":4,"amount":100000,"currency":"INR","approved_date":"2014-12-19","lockState":"Locked","id":7543,"withdrawal_request":0} ]
sample.xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,196" RenderTransformOrigin="0.5,0.98">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
</Grid.RowDefinitions
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid>
<TextBlock TextWrapping="Wrap" Text="TCN" Grid.Row="0" Grid.Column="0" Name="aa" Foreground="Black" FontWeight="Bold" />
<TextBlock TextWrapping="Wrap" Text="{binding scheme}" Grid.Row="0" Grid.Column="1" Name="aa" Foreground="Black" FontWeight="Bold" />
<TextBlock TextWrapping="Wrap" Text="UNIT" Grid.Row="1" Grid.Column="0" Name="aa" Foreground="Black" FontWeight="Bold" />
<TextBlock TextWrapping="Wrap" Text="{binding unit}" Grid.Row="0" Grid.Column="1" Name="aa" Foreground="Black" FontWeight="Bold" />
</Grid>
</Grid>
</Grid>
sample.xaml.cs
{
var users = JArray.Parse(data.Result.ToString());
tcnobjects1 = new TcnObjects1 {scheme = users[0]["scheme"].ToString(), units = users[0]["units"].ToString(), amount = users[0]["amount"].ToString(), currency = users[0]["currency"].ToString(), lockState = users[0]["lockState"].ToString(), id = users[0]["id"].ToString(), withdrawal_request = users[0]["withdrawal_request"].ToString()};
ContentPanel.DataContext = tcnobjects1;
}
如何循环网格中的项目?我正在动态获取json数据。使用上面的代码我只能显示Json数组 users [0] 元素。帮我看一下示例代码示例。
答案 0 :(得分:-1)
在JArray的返回类型中是一个数组,因此您可以获取数组的长度并循环它们,如下所示
//creating a list for tcnobjects
List<tcnobjects1> objectsList=new List<tcnobjects1>();
var users = JArray.Parse(data.Result.ToString());
//looping through the array elements
for (int i = 0; i < users.Length; i++)
{
tcnobjects1 = new TcnObjects1 {scheme = users[i]["scheme"].ToString(), units = users[i]["units"].ToString(), amount = users[i]["amount"].ToString(), currency = users[i]["currency"].ToString(), lockState = users[i] ["lockState"].ToString(), id = users[i]["id"].ToString(), withdrawal_request = users[i]["withdrawal_request"].ToString()};
objectsList.add(tcnobjects1);
}
ContentPanel.DataContext =objectsList
我希望我的回答很有帮助:)