我正在开发一个WP8应用程序,我正在尝试使用LongListSelector(作为People应用程序)在JumpList中显示所有用户联系人。
它工作正常,但根据联系人的数量,用户界面需要很长时间才能显示。
我这样做:
XAML:
<DataTemplate x:Key="AddrBookItemTemplate" >
<ListBoxItem>
<StackPanel VerticalAlignment="Top" >
<Border Height="62" Width="62">
<Image Source="{Binding Path=Image}" Width="62" Height="62" Stretch="Fill" />
</Border>
<TextBlock Text="{Binding Path=Name, Mode=OneWay}" />
</StackPanel>
</ListBoxItem>
</DataTemplate>
<DataTemplate x:Key="AddrBookGroupHeaderTemplate">
<Border Background="Transparent" Margin="0,5,0,0">
<Border Width="62" Height="62" >
<TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" />
</Border>
</Border>
</DataTemplate>
<phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter" Enabled="#FF03A9F4"/>
<phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/>
<Style x:Key="AddrBookJumpListStyle" TargetType="phone:LongListSelector">
<Setter Property="GridCellSize" Value="113,113"/>
<Setter Property="LayoutMode" Value="Grid" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Width="113" Height="113" Margin="6" >
<TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Padding="6"
Foreground="{Binding Converter={StaticResource ForegroundConverter}}" VerticalAlignment="Center"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,10,12,10">
<phone:LongListSelector
x:Name="AddrBook"
JumpListStyle="{StaticResource AddrBookJumpListStyle}"
Background="Transparent"
GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"
ItemTemplate="{StaticResource AddrBookItemTemplate}"
LayoutMode="List"
IsGroupingEnabled="true"
HideEmptyGroups ="true"
SelectionChanged="AddrBook_SelectionChanged"/>
</Grid>
代码:
private void Contacts_SearchContactsCompleted(object sender, ContactsSearchEventArgs e)
{
Stopwatch stopwatch = Stopwatch.StartNew();
IList<Contact> contacList = e.Results.Where(m => m.PhoneNumbers.Count() > 0).ToList();
cdList = new List<ContactData>();
foreach(Contact c in contacList)
{
BitmapImage img = new BitmapImage();
img.DecodePixelHeight = 62;
img.DecodePixelWidth = 62;
img.UriSource = null;
bool tam = c.GetPicture() == null;
if (!tam)
img.SetSource(c.GetPicture());
foreach (ContactPhoneNumber cpn in c.PhoneNumbers)
{
ContactData cd = new ContactData();
cd.Name = c.DisplayName;
cd.Image = img;
cdList.Add(cd);
}
}
List<AlphaKeyGroup<ContactData>> DataSource = AlphaKeyGroup<ContactData>.CreateGroups(cdList,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(ContactData s) => { return s.Name; }, true);
AddrBook.ItemsSource = DataSource;
cdListFiltred = cdList;
stopwatch.Stop();
System.Diagnostics.Debug.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
我试图用这个“秒表”测量时间,以验证代码是否是问题,但它返回“Time elapsed:00:00:00.3080245”,并且联系人在视觉上花费的时间比这更多出现在用户界面上。
然后,是XAML的问题?如何更快地显示此列表?
修改
我在这里上传了我的代码,如果能提供帮助:https://www.dropbox.com/s/k9c63xgi0wbf6r6/ContactsInJumpList.rar?dl=0
答案 0 :(得分:1)
问题在于XAML。当数据量很大时,性能很差。您可以使用虚拟化来提高性能,快速谷歌搜索虚拟化longlistselector应该会给你带来结果。我建议使用Telerik控件来处理大量数据,因为控件本身是虚拟化的。