【问题标题】:How to Create grid child elements using for loop dynamically?如何使用for循环动态创建网格子元素?
【发布时间】:2015-01-24 05:11:58
【问题描述】:

我是 windows phone 开发的新手。我被这个问题困住了。我想通过使用绑定 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] 元素。帮助我提供示例代码.

【问题讨论】:

标签: c# json xaml windows-phone-8


【解决方案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

希望我的回答对你有帮助:)

【讨论】:

  • 出现绑定错误:System.Windows.Data 错误:BindingExpression 路径错误:在“System.Collections.Generic.List1[Myapp.pendingtcn+TcnObjects1]' 'System.Collections.Generic.List1[Myapp.pendingtcn+TcnObjects1]”上找不到“scheme”属性(哈希码=49347015)。 BindingExpression: Path='scheme' DataItem='System.Collections.Generic.List`1[Myapp.pendingtcn+TcnObjects1]' (HashCode=49347015);目标元素是'System.Windows.Controls.TextBlock'(名称='ab');目标属性是“文本”(类型“System.String”)..
  • 你是否在 objectsList 中获取数据?只需检查断点,我的解决方案是循环遍历 json 中的所有项目
  • 如何在 xaml 中显示所有项目?
  • var t = JObject.Parse(result); var x= JsonConvert.SerializeObject(t); XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(x);这是我用来转换 json 数据的一种方式
猜你喜欢
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多