TextBox可搜索的长列表选择器在WindowsPhone8中显示内存不足异常

本文关键字:WindowsPhone8 显示 内存不足 异常 选择器 搜索 列表 TextBox | 更新日期: 2023-09-27 17:59:56

我有一个长列表选择器,在页面加载中将数据分配为ItemSource。如下图所示,

objectType = "mobilecontacts";
Contacts = (List<AllMobileContacts>)PhoneApplicationService.Current.State["Contacts"];
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    peopleLongListSelector.Visibility = Visibility.Visible;
    chatpeopleLongListSelector.Visibility = Visibility.Collapsed;
    peopleLongListSelector.ItemsSource= Contacts;
});

在文本框文本更改事件中,

private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
    progressBar.IsIndeterminate = true;
    TextBox tb = (TextBox)sender;
    string s = tb.Text;
    if (Contacts != null)
    {
        var searchName = 
                 (from name in Contacts
                       where name.USER_NAME.ToLower().Contains(s.ToLower())
                             select name).ToList();
        this.peopleLongListSelector.ItemsSource = null;
        this.peopleLongListSelector.ItemsSource = searchName;
    }
    else if (ChatContacts != null)
    {
        var searchName = 
                (from name in ChatContacts
                      where name.USERNAME.ToLower().Contains(s.ToLower())
                            select name).ToList();
        this.chatpeopleLongListSelector.ItemsSource = searchName;
    }
    progressBar.IsIndeterminate = false;
}

一切正常,但当更改文本2-3次时,它显示OutOfMemory异常
因为这个列表有图片和一堆文本
我该如何解决这个问题。。

这是我的数据模板长名单选择器:

<phone:LongListSelector Name="chatpeopleLongListSelector" Visibility="Collapsed">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stackpanel" Orientation="Horizontal" Margin="10,0" 
                        Height="75" Width="450" Tap="ucSearch_Tap">
                <StackPanel Width="392" Orientation="Horizontal" HorizontalAlignment="Left">
                    <StackPanel Width="62">
                        <Image Height="62" Width="62" VerticalAlignment="Top" Name="profileimg"
                               Source="{Binding PROFILEPIC,Converter={StaticResource imageConverter}}" />
                    </StackPanel>
                    <StackPanel x:Name="stpUserName" Width="330" Orientation="Vertical">
                        <TextBlock x:Name="txtbUserName" Text="{Binding USERNAME}" 
                                   Style="{StaticResource PhoneTextLargeStyle}" FontSize="28"
                                   Foreground="#FF6B6D70" 
                                   FontFamily="/Assets/Fonts/ProximaNova-Light_0.otf#Proxima Nova" 
                                   VerticalAlignment="Center" HorizontalAlignment="Left" />
                        <TextBlock x:Name="txtNumber" Opacity="0.7" Text="{Binding From}" 
                                   Style="{StaticResource PhoneTextLargeStyle}" 
                                   Foreground="#FF6B6D70"
                                   FontFamily="/Assets/Fonts/ProximaNova-Light_0.otf#Proxima Nova" FontSize="16" 
                                   VerticalAlignment="Center" HorizontalAlignment="Left" />
                        <TextBlock x:Name="txChatType" Visibility="Collapsed" Opacity="0.7" 
                                   Text="{Binding IsChatType}" Foreground="#FF6B6D70"
                                   Style="{StaticResource PhoneTextLargeStyle}" 
                                   FontFamily="/Assets/Fonts/ProximaNova-Light_0.otf#Proxima Nova" 
                                   FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Left" />
                    </StackPanel>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

TextBox可搜索的长列表选择器在WindowsPhone8中显示内存不足异常

Simple我使用了ListBox控件而不是LongListSelector,问题消失了。