【发布时间】:2015-05-12 11:35:42
【问题描述】:
如何将images[1]的宽度和高度为300x300的图像绑定到xaml?
public class TrackSpotify
{
public class ExternalUrls
{
public string spotify { get; set; }
}
public class Image
{
public int height { get; set; }
public string url { get; set; }
public int width { get; set; }
}
public class Item
{
public string album_type { get; set; }
public List<string> available_markets { get; set; }
public ExternalUrls external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public List<Image> images { get; set; }
public string name { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class Albums
{
public string href { get; set; }
public List<Item> items { get; set; }
public int limit { get; set; }
public string next { get; set; }
public int offset { get; set; }
public object previous { get; set; }
public int total { get; set; }
}
public class RootObject
{
public Albums albums { get; set; }
}
}
"images" : [ {
"height" : 640,
"url" : "i.scdn.co/image/8642802d13a53541e313781c34521a0d33099aac",
"width" : 640
}, {
"height" : 300,
"url" : "..................",
"width" : 300
}, {
"height" : 64,
"url" : "....................",
"width" : 64
} ],
<Grid.RowDefinitions>
<RowDefinition Height="200"></RowDefinition>
<RowDefinition Height="140"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Image Source="{Binding url}"></Image>
</StackPanel>
<Grid Grid.Row="1">
<StackPanel VerticalAlignment="Top" Margin="10">
<TextBlock Text="{Binding name}" Foreground="#D4D4D4" FontSize="12"></TextBlock>
<TextBlock Text="{Binding album_type}" Foreground="#404040" FontSize="15" FontWeight="SemiBold"></TextBlock>
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
var arrays = JsonConvert.DeserializeObject<NewReleaseSpotify.RootObject>(query);
spotifyItems.ItemsSource = arrays.albums.items;
【问题讨论】:
-
spotifyItems在哪里声明? (用于最后一行代码)您可以尝试使用ItemsControl来显示图像列表 -
你有什么问题?从你的一句话中很难说出来。
-
arrays.albums.items中的每个Item可能有多个图像 (List<Image>),因此您可能希望用ListBox或其他东西替换单个Image控制您的 XAML。无论如何,为了让你开始,试着只显示第一张图片:<Image Source="{Binding images[0].url}">