值不在预期范围内.在c#中绑定JSON对象时
本文关键字:绑定 JSON 对象 范围内 | 更新日期: 2023-09-27 18:13:00
我正在测试来自MovieDB的连接,并将电影数据绑定到两个参数。但是我只得到这个错误:
"值不在预期范围内"。
我不知道为什么这个错误被抛出…任何想法吗?
如果我在MovieList.ItemsSource = deserialized
之前设置一个断点,我可以看到它包含了来自电影的所有数据…
<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;
}
}
}
电影类: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; }
}
尝试更改
<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>
和你的代码
this.DataContext = movieItem;