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

发布于 2024-12-20 11:14:08 字数 1064 浏览 2 评论 0原文

我正在尝试从网站加载新闻列表,因此首先请求获取新闻(带有缩略图),然后使用绑定功能,将获取的新闻分配给我的列表框,其中包含图像(图像网址)。

                <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>

它工作正常,但用户界面会冻结,直到图像显示为止。我该如何解决这个问题?

I'm trying to load a list of news from a website, so first I make a request to fetch the news (with the thumbnails), and using the Binding feature, I assign the Fetched news to my list box, which contains the image (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>

It works fine, but the UI freezes until the images show up. how can I fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑脸一如从前 2024-12-27 11:14:08

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

<ImageBrush>
    <ImageBrush.ImageSource>
        <BitmapImage CreateOptions="BackgroundCreation" UriSource="{Binding ImageUrl}"/>
    </ImageBrush.ImageSource>
</ImageBrush>

确保为 CreateOptions 指定 BackgroundCreation。有关更多信息,我个人找到了这个博客文章 非常有用。

Actually, BitmapImages by default do load on the UI thread, which would cause the blockage. In your xaml, you should be able to do something like:

<ImageBrush>
    <ImageBrush.ImageSource>
        <BitmapImage CreateOptions="BackgroundCreation" UriSource="{Binding ImageUrl}"/>
    </ImageBrush.ImageSource>
</ImageBrush>

Making sure to specify BackgroundCreation for the CreateOptions. For more information, I personally found this blog post to be quiet useful.

獨角戲 2024-12-27 11:14:08

您是否进行过测试以确保注释掉 ImageBrush 行时 UI 不会冻结?我相信自动转换会从您的 ImageUrl 创建一个 BitmapImage,它通常加载在后台线程上,因此您的 UI 线程不应阻塞。可能还有其他东西阻塞了 UI,例如需要布局的文本过多或为列表提供数据的数据层代码。

Have you tested to make sure that your UI does not freeze when you comment out your ImageBrush line? I believe the automatic conversion creates a BitmapImage from your ImageUrl, which normally loads on a background thread, so your UI thread should not block. It is possible that something else altogether is blocking the UI, like excessive amounts of text that needs to be laid out or your data layer code that feeds the list with the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文