2012-05-08 48 views
0

我試圖執行一個「無限」列表框,將達到列表框的末尾每次加載新的數據。當我嘗試反序列化下一組搜索結果時,就會出現問題。我得到一個Operation not supported on read-only collectionWP7將數據添加到列表框中無限

這是我的課程。

public class Search 
{ 
    //public Spellcheck spellcheck { get; set; } 
    public ObservableCollection<ResultSearch> results = new ObservableCollection<ResultSearch>(); 

    public IEnumerator<ResultSearch> GetEnumerator() 
    { 
     return this.results.GetEnumerator(); 
    } 
} 

public class ResultSearch 
{ 
    public int id { get; set; } 
    public bool selected { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
    public bool active { get; set; } 
    public string mixName { get; set; } 
    public string title { get; set; } 
    public string slug { get; set; } 
    public object isrc { get; set; } 
    public string releaseDate { get; set; } 
    public string publishDate { get; set; } 
    public string sampleUrl { get; set; } 
    public string rtmpStreamUrl { get; set; } 
    public bool exclusive { get; set; } 
    public Price price { get; set; } 
    //public AudioFormatFee audioFormatFee { get; set; } 
    public string currentStatus { get; set; } 
    public string length { get; set; } 
    public int bpm { get; set; } 
    public Key key { get; set; } 
    public string saleType { get; set; } 
    public List<SearchArtist> artists { get; set; } 
    public List<SearchGenre> genres { get; set; } 
    public List<object> subGenres { get; set; } 
    public List<SearchChart> charts { get; set; } 
    public SearchRelease release { get; set; } 
    public SearchLabel label { get; set; } 
    public SearchImages images { get; set; } 
    //public DynamicImages dynamicImages { get; set; } 
    public string bio { get; set; } 
    public string url { get; set; } 
} 

public class SearchArtist 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string slug { get; set; } 
    public string type { get; set; } 
} 

public class SearchGenre 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string slug { get; set; } 
    public string type { get; set; } 
} 

public class SearchChart 
{ 
    public int id { get; set; } 
    public int position { get; set; } 
    public string type { get; set; } 
} 

public class SearchRelease 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public string slug { get; set; } 
} 

public class SearchLabel 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public string slug { get; set; } 
} 

public class SearchImages 
{ 
    //public Small small { get; set; } 
    //public SearchMedium medium { get; set; } 
    public SearchLarge large { get; set; } 
    //public SearchBanner banner { get; set; } 
    //public Waveform waveform { get; set; } 
} 

public class SearchLarge 
{ 
    public int width { get; set; } 
    public int height { get; set; } 
    public string url { get; set; } 
    public string secureUrl { get; set; } 
} 

反序列化的新數據

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 

     string searchTxt = String.Empty; 
     if (NavigationContext.QueryString.TryGetValue("searchTxt", out searchTxt)) 
     { 
      searchTxtBox.Text = searchTxt; 
      HttpUtility.UrlEncode(searchTxt); 
      LaunchWebClient(searchTxt); 
     } 
    } 

    private void LaunchWebClient(string searchTxt) 
    { 
     // WebClient jsonSearch 
     WebClient jsonSearch = new GzipWebClient(); 
     Uri apiSearch = new Uri("http://api.beatport.com/catalog/3/search?query=" + searchTxt); 
     jsonSearch.DownloadStringCompleted += new DownloadStringCompletedEventHandler(jsonSearch_GetDataCompleted); 
     jsonSearch.DownloadStringAsync(apiSearch); 
    } 

    // When end of listbox is reached 
    private void LaunchWebClientNextpage(int page) 
    { 
     int pageLimit = page; 
     if (pageLimit <= 5) 
     { 
      // WebClient jsonSearch 
      WebClient jsonSearch = new GzipWebClient(); 
      Uri apiSearch = new Uri("http://api.beatport.com/catalog/3/search?query=" + searchTxtBox.Text + "&page=" + page); 
      jsonSearch.DownloadStringCompleted += new DownloadStringCompletedEventHandler(jsonSearchNextPage_GetDataCompleted); 
      jsonSearch.DownloadStringAsync(apiSearch); 
     } 
     else 
     { 
      SystemTray.SetIsVisible(this, false); 
      //SystemTray.SetOpacity(this, 0.0); 
      prog.IsVisible = false; 
      prog.IsIndeterminate = false; 
     } 
    } 

    // Deserialize search json data 
    public void jsonSearch_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     Search searchData = JsonConvert.DeserializeObject<Search>(e.Result); 

     if (searchData.results.Count == 0) 
     { 
      MessageBox.Show("your search didn't return any results"); 
     } 
     else 
     { 
      this.listSearch.ItemsSource = searchData.results; 
     } 
    } 

    // Deserialize search next page json data 
    public void jsonSearchNextPage_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     Search searchDataPage = JsonConvert.DeserializeObject<Search>(e.Result); 

     //listSearch.Items.Add(searchDataPage.results); 
    } 

感謝您的幫助。

UPDATE

這是我的XAML以及

<!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">    
     <ListBox x:Name="listSearch" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" > 
         <Image delay:LowProfileImageLoader.UriSource="{Binding images.medium.url}" Tap="searchSelectedImageHandler" Margin="10" Width="60" Height="60" /> 
         <StackPanel Orientation="Vertical" > 
          <TextBlock Text="{Binding name}" Tap="searchSelectedHandler" Style="{StaticResource PhoneTextLargeStyle}"/> 
          <TextBlock Text="{Binding type}" Style="{StaticResource PhoneTextSubtleStyle}" /> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

回答

0

張貼的代碼有一個class SearchObservableCollection

public ObservableCollection<ResultSearch> results = new ObservableCollection<ResultSearch>(); 

但是,反序列化正試圖增加a listSearch

listSearch.Items.Add(searchDataPage.results); 

我懷疑,該項目應添加到results集合,而不是listSearch控制。

編輯迴應評論

最快的(不是最理想的)事情做的,是建立在你的代碼隱藏一個ObservableCollection,然後設置ListBox的的ItemsSource到的ObservableCollection和修改JSON方法:

// code behind for your xaml 
public partial class MyCodeBehind 
{ 
    public ObservableCollection<Search> Searches; 

    public MyCodeBehind() 
    { 
    InitializeComponent(); 

    Searches = new ObservableCollection<Search>(); 
    // bind the collection to the ListBox 
    this.listSearch.ItemsSource = Searches; 
    } 

    // Deserialize search json data 
    public void jsonSearch_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
    Search searchData = JsonConvert.DeserializeObject<Search>(e.Result); 

    if (searchData.results.Count == 0) 
    { 
     MessageBox.Show("your search didn't return any results"); 
    } 
    else 
    { 
     // add the results to the existing Searches collection 
     foreach (Search search in searchData.results) 
     { 
     Searches.Add(search); 
     } 
    } 
    } 

    // Deserialize search next page json data 
    public void jsonSearchNextPage_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
    Search searchDataPage = JsonConvert.DeserializeObject<Search>(e.Result); 

    // add the results to the existing Searches collection 
    foreach (Search search in searchDataPage) 
    { 
     Searches.Add(search); 
    } 
    } 
} 
+0

感謝您的回答。我很難弄清楚這一點。你能提供一個例子嗎? – nos9

+0

查看修改。如果這不起作用,則需要添加將集合綁定到列表框的其他代碼。 –

+0

我得到了'當前上下文中不存在的結果'錯誤。另請參閱我的xaml更新 – nos9