2015-09-15 1 views
0

J'essaie de tester la connexion d'un MovieDB et de lier une donnée de films à deux paramètres. Mais je ne reçois que cette erreur:"La valeur ne se situe pas dans la plage attendue" lors de la liaison d'un objet JSON en C#

"Value does not fall within the expected range".

Je ne peux pas comprendre pourquoi cette erreur est jeté ... toutes les pensées?

Si je fais un bon point d'arrêt avant la MovieList.ItemsSource = deserialized je peux voir qu'il contient toutes les données du film ...

<StackPanel> 
    <ListBox x:Name="MovieList" Height="532"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding original_title}" 
       Margin="0,0,12,0" /> 
        <TextBlock Text="{Binding poster_path}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</StackPanel> 

private async void KopplaDB() 
{ 
    var baseAddress = new Uri("https://api.themoviedb.org/3/"); 
    var key = "?api_key=*************************************"; 
    using (var httpClient = new HttpClient { BaseAddress = baseAddress }) 
    {   
      httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); 

      using (var response = await httpClient.GetAsync("movie/550" + key)) 
      { 
       string responseData = await response.Content.ReadAsStringAsync(); 
       var deserialized = JsonConvert.DeserializeObject<Movie>(responseData); 
       MovieList.ItemsSource = deserialized; 
      } 
    } 
} 

classe Movie:

class Movie 
{ 
    public bool adult { get; set; } 
    public string backdrop_path { get; set; } 
    public object belongs_to_collection { get; set; } 
    public int budget { get; set; } 
    public List<Genre> genres { get; set; } 
    public string homepage { get; set; } 
    public int id { get; set; } 
    public string imdb_id { get; set; } 
    public string original_language { get; set; } 
    public string original_title { get; set; } 
    public string overview { get; set; } 
    public double popularity { get; set; } 
    public string poster_path { get; set; } 
    public List<ProductionCompany> production_companies { get; set; } 
    public List<ProductionCountry> production_countries { get; set; } 
    public string release_date { get; set; } 
    public int revenue { get; set; } 
    public int runtime { get; set; } 
    public List<SpokenLanguage> spoken_languages { get; set; } 
    public string status { get; set; } 
    public string tagline { get; set; } 
    public string title { get; set; } 
    public bool video { get; set; } 
    public double vote_average { get; set; } 
    public int vote_count { get; set; } 
} 
+3

Pouvez-vous fournir toute erreur/stacktrace? – JNYRanger

+0

StackTrace \t "à Windows.UI.Xaml.Controls.ItemsControl.put_ItemsSource (Valeur de l'objet) \ r \ n à MovieTime.MainPage d__1.MoveNext() \ r \ n --- Fin de la trace de la pile de l'emplacement précédent où exception a été levée --- \ r \ n à System.Runtime.CompilerServices.AsyncMethodBuilderCore. <> c__DisplayClass2. b__3 (état de l'objet) \ r \ n à System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore() "\t chaîne – Newbie1337

+0

peut nous voyons vos propriétés dans l'objet 'Movie'? Je suppose que quelque chose est marqué comme un type qu'il ne devrait pas, donc il ne désérialise pas correctement. – Greg

Répondre

1

Essayez de changer cela

<StackPanel> 
<ListBox x:Name="MovieList" Height="532"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding original_title}" 
      Margin="0,0,12,0" /> 
       <TextBlock Text="{Binding poster_path}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

à

  <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding original_title}" 
      Margin="0,0,12,0" /> 
       <TextBlock Text="{Binding poster_path}"/> 
      </StackPanel> 

et dans votre code

this.DataContext = movieItem;