简体   繁体   中英

How can i convert list task to Image in WPF

This is My API in xaml.cs I'm get image from API this code i'm get API path in var medias and

private async void Start()
    {
        var medias = await GetAPIAsync("http://139.59.229.66:5004/api/v1/Medias/Getinfo1"); //this my API to get Image
        var items = new List<ImageSource>();//this creat new List 
        foreach (var item in medias)
        {
            items.Add(new BitmapImage(new Uri("http://" + item))); //this loop to get Image
        }
        ImageViewer.ItemsSource = items; 
    }

My List xaml.cs

static async Task<List<string>> GetAPIAsync(string path)
    {
        HttpResponseMessage response = await client.GetAsync(path);
        List<MediasResult> mediaslist = new List<MediasResult>();
        if (response.IsSuccessStatusCode)
        {
            string jsonResponse = await response.Content.ReadAsStringAsync();
            mediaslist = JsonConvert.DeserializeObject<List<MediasResult>>(jsonResponse).ToList();
        }
        var list = new List<string>();
        foreach (var item in mediaslist)
        {
            list.Add(item.MediaName);
        }
        return list;
    }

And I'm call this to xaml

  <ListBox x:Name="ImageViewer" Margin="170,152,172,62"
                             ScrollViewer.VerticalScrollBarVisibility="Disabled"
                             ScrollViewer.HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Center">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel IsItemsHost="True"/>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                    <Image Source="{Binding}" Width="1280" Height="720"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

Which one should use WPF Control tool ?

Thank you for your guidance

Your API seems to return a list of objects, where the image url seems stored in the property "mediaName" or ("MediaName"?). At least that's what I think when browsing to http://139.59.229.66:5004/api/v1/Medias/Getinfo1

So probably you want:

items.Add(new BitmapImage(new Uri($"http://{item.mediaName}")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM