在Windows手机上加载xml并绑定到ListBox非常慢

时间:2014-06-28 19:44:48

标签: xml wpf xaml windows-phone-8 windows-phone

我读了这个xml文件并将其绑定到listBox:

<?xml version="1.0" encoding="utf-8"?>
<category name="category_alphabet" type="grid" icon="icon_alphabet" tabStyle="alphabetic"    tabStepSize="5">
<item>
    <id>dk1-1</id>
    <label>A</label>
    <file>a</file>
</item>
<item>
    <id>dk1-2</id>
    <label>B</label>
    <file>b</file>
</item>
.
.
.

它有29个项目

我用这些代码读取了这个xml文件,我在page1.xaml构造函数中的InitializeComponent()之后调用它:

var loadedData = XDocument.Load("alphabet.xml").Descendants("item");

var alphabetItems = (from query in loadedData select new CategoryItem
{
    Id = (string)query.Element("id"),
    File = (string)query.Element("file"),
    Label = (string)query.Element("label")
}).ToList();

ListBoxAlphabet.ItemsSource = alphabetItems;

这是xaml文件:

<ListBox x:Name="ListBoxAlphabet">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <toolkit:WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <controls:RoundButton Content="{Binding Label}"
                                      FontSize="30" Margin="1" 
                                      ButtonHeight="80" ButtonWidth="80"
                                      HorizontalAlignment="Center" 
                                      RenderTransformOrigin="0.5,0.5"
                                      Background="#FFD0D2D3" Foreground="White"
                                      PressedBrush="#F9A11D" 
                                      BorderBrush="{Binding Background.Color, RelativeSource={RelativeSource Self}}" 
                                      />
             </StackPanel>
         </DataTemplate>
     </ListBox.ItemTemplate>
 </ListBox>

读取xml文件的速度还可以,但绑定到列表框的速度非常慢!加载需要7-8秒,这是不正常的。 我该怎么办?

0 个答案:

没有答案