从API获取图像列表并显示列表中的每个图像

时间:2016-05-18 19:10:01

标签: c# api xaml uwp

我正在使用API​​来获取Json数据,我想显示从集合中获取的每张卡片中的每张图片,我可以从card.imageUrl获取图片网址,但图片来源不会占用常规字符串。我试图在foreach循环中写入card.imageUrl = String.Format(card.imageUrl)但是它不起作用

XAML代码:

        <ListView Name="MasterListView" ItemsSource="{x:Bind MagicCards}">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Card">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{x:Bind ImageUrl}"/>
                    <TextBlock Margin="20,0,0,0" Text="{x:Bind Name}" Foreground="{x:Bind Colors[0]}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

添加到MagicCardsList

       public static async Task PopulateMagicCardsAsync(ObservableCollection<Card> magicCards)
    {
        var cardDataWrapper = await GetCardDataWrapperAsync();

        var cards = cardDataWrapper.cards;

        foreach (var card in cards)
        {
            if (card.Multiverseid.ToString() != null && card.Multiverseid.ToString() != "")
            {
                magicCards.Add(card);
            }
        }
    }

主页

    public sealed partial class MainPage : Page
{
    public ObservableCollection<Card> MagicCards { get; set; }
    public MainPage()
    {
        this.InitializeComponent();

        MagicCards = new ObservableCollection<Card>();       
    }
    private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
        MyProgressRing.IsActive = true;
        MyProgressRing.Visibility = Visibility.Visible;

        await MTGFacade.PopulateMagicCardsAsync(MagicCards);

        MyProgressRing.IsActive = false;
        MyProgressRing.Visibility = Visibility.Collapsed;
    }
}

卡类

    public class Card
{
    [DataMember(Name = "imageUrl")]
    public string ImageUrl { get; set; }

编辑: http://i.imgur.com/yqQHdTp.png

1 个答案:

答案 0 :(得分:1)

虽然x:Bind要求完全匹配源和目标属性的类型,但Binding可以从内置自动类型转换中受益(此处从stringImageSource)。

所以改变

<Image Source="{x:Bind ImageUrl}"/>

<Image Source="{Binding ImageUrl}"/>