内存不足异常在Windows Phone 8上使用LongListSelector时 [英] Out of Memory exception While using LongListSelector on Windows Phone 8

查看:106
本文介绍了内存不足异常在Windows Phone 8上使用LongListSelector时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我正在使用LongListSelector向用户显示歌曲(它们位于服务器上)。


下面是用于LongListSelector的 ItemTemplate

< DataTemplate x:Key =" AppTopItemsTemplate"> 
< Grid Margin =" 10">
< Grid.RowDefinitions>
< RowDefinition Height =" *" />
< RowDefinition Height =" 50" />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =" *">< / ColumnDefinition>
< /Grid.ColumnDefinitions>

< Image Name =" itemImage" Grid.Row = QUOT; 0" Grid.Column = QUOT; 0" Grid.RowSpan = QUOT; 2英寸伸展= QUOT; UniformToFill"
>
< Image.Source>
< BitmapImage UriSource =" {Binding ImageUrl}"
CreateOptions =" BackgroundCreation" />
< /Image.Source>
< / Image>
< StackPanel Grid.Row =" 1" Grid.Column = QUOT; 0"取向= QUOT;垂直"背景= QUOT;#99000000" >

< TextBlock Name =" itemTitle" Style =" {StaticResource AppItemTitleSubtitle}"
Text =" {Binding Title}"
>< / TextBlock>
< TextBlock Name =" itemSubTitle" Style =" {StaticResource AppItemTitleSubtitle}"
Text =" {Binding SubTitle}"填充= QUOT; 0,0,0,5" >< / TextBlock的>
< / StackPanel>
< / Grid>
< / DataTemplate>



ImageUrl 是图像控制的远程图像网址。


我实现了LongListSelector,下面是无限滚动代码。

 private void List_ItemRealized_1(object sender,ItemRealizationEventArgs e)
{
if(songList.ItemsSource as ObservableCollection< SongTable> == null)
{
return;
}
int indexofRealizeditem = songsDataSource.Items.IndexOf(sngItem);
if(!songsDataSource.IsLoading&&(e.Container.Content as SongTable)!= null&& e.ItemKind == LongListSelectorItemKind.Item&&(indexofRealizeditem == currentCount - 1) )
{
//一旦实现最后一项我将从server.loadmore加载更多项目是异步调用它不会阻止ui线程。
songsDataSource.loadMore();
}
}

一切正常,但是当LongListSelector中加载超过 1000个项目时,它会抛出Out of Memory Exception。


由于我正在从服务器加载图片,这可能是原因但我如何为项目发布
内存这是由LongListSelector未实现的。


项目未实现时,我将图像控制的源设置为null但它没有帮助。


任何人都可以建议我如何 我可以解决这个问题。


为了您在我的服务器上的信息,我有大约10000项。


解决方案

嗨Ashwin,



鉴于此包含图像的行的高度为50,您可以尝试添加DecodePixelHeight和DecodePixelWidth以将图像采样到较小的尺寸(假设图像很大):


 
< Image.Source>

< BitmapImage UriSource =" {Binding ImageUrl}" DecodePixelHeight = QUOT; 32" DecodePixelWidth = QUOT; 32" />

< /Image.Source>


将32改为任何你想要的高度/宽度。


Andrew


Hi All

I am using LongListSelector to show songs(they are located on server) to the user.

Below is the ItemTemplate used for LongListSelector

<DataTemplate x:Key="AppTopItemsTemplate">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="50"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions >
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    
                    <Image  Name="itemImage" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Stretch="UniformToFill"
                                   >
                        <Image.Source>
                            <BitmapImage UriSource="{Binding ImageUrl}"
                                     CreateOptions="BackgroundCreation"/>
                        </Image.Source>
                    </Image>
                    <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" Background="#99000000"  >

                        <TextBlock Name="itemTitle" Style="{StaticResource AppItemTitleSubtitle}"
                                    Text="{Binding Title}"
                                  ></TextBlock>
                        <TextBlock Name="itemSubTitle"  Style="{StaticResource AppItemTitleSubtitle}"
                                    Text="{Binding SubTitle}" Padding="0,0,0,5" ></TextBlock>
                    </StackPanel>
                </Grid>
            </DataTemplate>


ImageUrl is the Remote Image Url for the Image Control.

I had implemented LongListSelector with infinite scrolling below is the code.

private void List_ItemRealized_1(object sender, ItemRealizationEventArgs e)
        {
            if (songList.ItemsSource as ObservableCollection<SongTable> == null)
            {
                return;
            }
            int indexofRealizeditem = songsDataSource.Items.IndexOf(sngItem);
            if (!songsDataSource.IsLoading && (e.Container.Content as SongTable) != null && e.ItemKind == LongListSelectorItemKind.Item && (indexofRealizeditem == currentCount - 1))
            { 
                // as soon as last item is realized I am loading more items from server.loadmore is async call it is not blocking ui thread.
                songsDataSource.loadMore();
            }
        }

Everything works fine for me But when more than 1000 items are loaded in LongListSelector It throws Out of Memory Exception.

Since I am Loading images from server this might be the cause but How will I release the Memory for items which are unrealized by LongListSelector.

When Items are unrealized I am setting source of Image Control to null but its not helping.

Can any one suggest me How  I can resolve this issue.

For you information on my server I am having around 10000 items.

解决方案

Hi Ashwin,

Given that the height of the row containing the image is 50, you could try adding DecodePixelHeight and DecodePixelWidth to dwonsample the image to a smaller size (assuming the images are large):

    <Image.Source>

        <BitmapImage UriSource="{Binding ImageUrl}" DecodePixelHeight="32" DecodePixelWidth="32"/>

    </Image.Source>

Change 32 to whatever you want as the height/ width.

Andrew


这篇关于内存不足异常在Windows Phone 8上使用LongListSelector时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆