2014-08-27 57 views
1

我想要使用我从web服务(成功)从web服务接收到的响应,并使用它在xaml端“创建”或“填充”列表...使用来自Webservice的响应来创建xaml列表

这里的JSON

{ 
    "success":true, 
    "code":200, 
    "rows":[ 
     { 
     "Purchase":{ 
      "id":"1" 
     }, 
     "Who":{ 
      "id":"1", 
      "value":"NA" 
     }, 
     "What":{ 
      "id":"1", 
      "value":"what-text" 
     } 
     }, 
     { 
     "Purchase":{ 
      "id":"2" 
     }, 
     "Who":{ 
      "id":"2", 
      "value":"ME" 
     }, 
     "What":{ 
      "id":"1", 
      "value":"what-text" 
     } 
     } 
    ] 
} 

,我也得到一个Webservice,从我的CS这样的..

HttpWebRequest hwr = rez.AsyncState as HttpWebRequest; 
HttpWebResponse response = hwr.EndGetResponse(rez) as HttpWebResponse; 
string jsonResponseString = (new StreamReader(response.GetResponseStream(), Encoding.UTF8)).ReadToEnd(); 

Dispatcher.BeginInvoke(() => 
{ 
    var responseObject = 
     Newtonsoft.Json.JsonConvert.DeserializeObject(jsonResponseString); 
}); 

其中一期工程。 “jsonResponseString”返回上面显示的json。现在

我想表明我的XAML网页上公布这些结果。在这里,我已经有什么的问题最好用..一个LongListSelector?或者应该一张桌子工作?

在我的CS我还设置:

public class Purchase 
{ 
    public List<string> Who { get; set; } 
    public List<string> What { get; set; } 
} 

public class RootObject 
{ 
    public List<Purchase> purchase { get; set; } 
    public int success { get; set; } 
    public string message { get; set; } 
} 

我在什么地方找到我可以用这些,但可能并不需要。

反正嘛,所以我很想弄清楚什么是最好的,我XAML视图中使用以及如何使用JSON返回的字符串或对象的数据填充到这个观点?

谢谢!

+0

master-detail视图可能有助于:http://en.wikipedia.org/wiki/Master%E2%80%93detail_interface – Aybe 2014-08-27 20:58:38

回答

3

你的类应该建立这样的匹配JSON结构:

public class Item 
{ 
    public int id { get; set; } 
    public string value { get; set; } 
} 

public class Row 
{ 
    public Item Purchase { get; set; } 
    public Item Who { get; set; } 
    public Item What { get; set; } 
} 

public class RootObject 
{ 
    public List<Row> rows { get; set; } 
    public bool success { get; set; } 
    public int code { get; set; } 
} 

然后你就可以反序列化到你输入“RootObject”:

RootObject responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonResponseString); 

至于演示,你可以使用任意数量的控件:

  • ListBox允许用户选择
  • LongListSelector像列表框,但有利于肥胖型集合
  • 如果你想只显示集合

比方说,你去与ListBox

  • ItemsControl是有用的 - 那么它只是一个设定的事其ItemsSource等于“responseObject.rows”,指定一个ItemTemplate。例如,这将显示“Who”和“What”列:

    <ListBox> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition /> 
            <ColumnDefinition /> 
           </Grid.ColumnDefinitions> 
           <TextBlock Grid.Column="0" Text="{Binding Who.value}" /> 
           <TextBlock Grid.Column="1" Text="{Binding What.value}" /> 
          </Grid> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
        <ListBox.ItemContainerStyle> 
         <Style TargetType="ListBoxItem"> 
          <Setter Property="HorizontalAlignment" Value="Stretch" /> 
          <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
         </Style> 
        </ListBox.ItemContainerStyle> 
    </ListBox> 
    
  • +0

    感谢您的答复和解释,我会得到它的工作,让你知道会发生什么。再次感谢! – 2014-08-27 23:10:17

    +0

    工作就像一个魅力!谢谢McGarnagle先生! – 2014-08-28 14:40:36