2014-08-27 56 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