从URL加载图像会冻结Windows手机上的UI

Ate*_*eik 1 url asynchronous dispatcher windows-phone-7

我正在尝试从网站加载新闻列表,所以首先我请求获取新闻(使用缩略图),并使用绑定功能,我将Fetched新闻分配到我的列表框,其中包含图像(的ImageUrl).

                <ListBox Name="lstNews">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,12,12"  Width="180" Height="180">
                                <Grid.Background>
                                    <ImageBrush ImageSource="{Binding ImageUrl}"  />
                                </Grid.Background>
                                <StackPanel Background="#AA000000" VerticalAlignment="Bottom" Height="60" >
                                    <TextBlock TextWrapping="Wrap" VerticalAlignment="Bottom" TextAlignment="Center" Text="{Binding Title}" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
Run Code Online (Sandbox Code Playgroud)

它工作正常,但UI冻结,直到图像出现.我该如何解决这个问题?

小智 6

实际上,默认情况下,BitmapImages会加载UI线程,这会导致阻塞.在您的xaml中,您应该能够执行以下操作:

<ImageBrush>
    <ImageBrush.ImageSource>
        <BitmapImage CreateOptions="BackgroundCreation" UriSource="{Binding ImageUrl}"/>
    </ImageBrush.ImageSource>
</ImageBrush>
Run Code Online (Sandbox Code Playgroud)

确保为CreateOptions指定BackgroundCreation.有关更多信息,我个人认为这篇博文非常有用.