无法从网上加载大量图片

时间:2017-11-10 19:51:21

标签: c# wpf xaml

我正在尝试使用WPF进行简单的Instagram Feed显示。我有一个ListView,它绑定到从Instagram API检索到的图像url的ObservableCollection。出于某种原因,我只能加载前10个左右(即使我试图绑定更多)。如果我尝试加载较低分辨率的图像,那么它似乎工作正常。但我不是在谈论大规模的图像(可能大约每个100k)。

以下是一些示例代码,其中包含来自instagram的硬编码图像列表。对我来说,这只会加载前10个,然后错过11个,然后加载12个。我不知道为什么。

XAML:

<ListView x:Name="List" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="250" Height="250" Background="Orange">
                <Image Margin="3" Source="{Binding}" Stretch="Uniform" VerticalAlignment="Center" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

代码背后:

List<string> content = new List<string>()
{
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23498621_155088151891201_5838888733310451712_n.jpg",      
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/e15/23421369_1935881716650536_2751759012239245312_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23498900_2424691114423634_1760461413358239744_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23498900_2424691114423634_1760461413358239744_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23347618_263128534210376_920911369891479552_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421069_1500830839998934_8413516091602501632_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23417348_742804169243503_6556496499100876800_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23347902_2017564028488677_2880579113104441344_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421331_334537296949861_4296430067371737088_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421331_334537296949861_4296430067371737088_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23416590_1928818560779337_7080637368553701376_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/p640x640/23421758_491705774540588_6178820534202007552_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421099_1728264697245367_6448825186568372224_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23416994_133163334113394_1499431022142423040_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421239_1483718185060885_8134914164545028096_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23280089_1942832749376286_3936575794389712896_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23347674_2006888666245374_7306582919252803584_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23347388_119626992143009_7585283007787827200_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421865_900301160123071_1326331715420946432_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23421253_148762415872849_1558483017121398784_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/p640x640/23507217_1953269574998281_6711372701967056896_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/p640x640/23507217_1953269574998281_6711372701967056896_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23498098_2009691429314434_8349013357923139584_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/e15/23347679_315062128974030_5091325576380153856_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/p640x640/23421083_1777350415899180_3004366087824343040_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/p640x640/23421083_1777350415899180_3004366087824343040_n.jpg",
    "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/23498903_773821689486761_9202713497564086272_n.jpg"
};

List.ItemsSource = content;

1 个答案:

答案 0 :(得分:1)

您正在遇到应用程序的同一主机的最大连接限制。

在app.config文件中,您可以在配置下添加以下内容:

<system.net>
  <connectionManagement>
    <add address="*" maxconnection="1000" />
  </connectionManagement>
</system.net>

您可以阅读有关此here的更多信息。显然,您应该根据应用程序的实际需要量身定制。