2011-08-03 26 views
1

之間切換當切換到該轉動加載數據項目,從一個項目切換到另一個時,我遇到口吃時刪除「口吃」。我已經將數據加載到一個單獨的線程,這有幫助,但我仍然遇到一些糟糕的表現....想知道如果你們有任何想法....Windows Phone 7的我怎麼樞紐項目

這裏是關鍵項目

<Grid x:Name="LayoutRoot" Background="Transparent"> 

    <!--Pivot control--> 
    <controls:Pivot Name="panCorals" Title="Corals" Foreground="#01487e" 
     SelectionChanged="panCorals_SelectionChanged"> 

     <controls:Pivot.Background> 
      <ImageBrush ImageSource="PivotBackground.png"/> 
     </controls:Pivot.Background> 

     <!--Search Corals--> 
     <controls:PivotItem Header="Search" Foreground="#01487e"> 

      <Grid> 

       <toolkit:PerformanceProgressBar Name="SearchCoralsProgressBar" HorizontalAlignment="Left" 
         VerticalAlignment="Top" Width="466" 
         IsEnabled="{Binding IsSearchLoading}" IsIndeterminate="{Binding IsSearchLoading}" /> 

       <StackPanel> 
        <TextBox Name="txtSearchTerm" KeyDown="txtSearchTerm_KeyDown" /> 
        <ListBox Name="lbSearchCorals" Margin="0,0,-12,0" ItemsSource="{Binding SearchCorals}" 
         SelectionChanged="lbSearchCorals_SelectionChanged"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal" Margin="0,0,0,17"> 
            <Image Source="{Binding MainImageURI}" Height="100" Width="100" Margin="12,0,9,0" /> 
            <StackPanel Width="311"> 
             <TextBlock Text="{Binding CommonName}" Foreground="#112d42" TextWrapping="NoWrap" Style="{StaticResource PhoneTextTitle2Style}"/> 
             <TextBlock Text="{Binding ScientificName}" Foreground="#112d42" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
            </StackPanel> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
       </StackPanel> 
      </Grid> 

     </controls:PivotItem> 


     <!--Top Corals--> 
     <controls:PivotItem Header="Top" Foreground="#01487e" VerticalAlignment="Top" > 

      <Grid> 

       <toolkit:PerformanceProgressBar Name="TopCoralsProgressBar" HorizontalAlignment="Left" 
         VerticalAlignment="Top" Width="466" 
         IsEnabled="{Binding IsTopLoading}" IsIndeterminate="{Binding IsTopLoading}" /> 


       <ListBox Name="lbTopCorals" Margin="0,0,-12,0" ItemsSource="{Binding TopCorals}" SelectionChanged="lbTopCorals_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,17"> 
           <Image Source="{Binding MainImageURI}" Height="100" Width="100" Margin="12,0,9,0" /> 
           <StackPanel Width="311"> 
            <TextBlock Text="{Binding CommonName}" Foreground="#112d42" TextWrapping="NoWrap" Style="{StaticResource PhoneTextTitle2Style}"/> 
            <TextBlock Text="{Binding ScientificName}" Foreground="#112d42" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
           </StackPanel> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 

       <StackPanel Margin="10,50,0,0" Orientation="Horizontal"> 
        <TextBlock x:Name="tbProgress"/> 
       </StackPanel> 

      </Grid> 

     </controls:PivotItem> 

     <!--New Corals--> 
     <controls:PivotItem Header="New"> 
      <Grid> 

       <toolkit:PerformanceProgressBar Name="NewCoralsProgressBar" HorizontalAlignment="Left" 
          VerticalAlignment="Top" Width="466" 
          IsEnabled="{Binding IsNewLoading}" IsIndeterminate="{Binding IsNewLoading}" /> 

       <ListBox Name="lbNewCorals" Margin="0,0,-12,0" ItemsSource="{Binding NewCorals}" SelectionChanged="lbNewCorals_SelectionChanged"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,17"> 
           <Image Source="{Binding MainImageURI}" Height="100" Width="100" Margin="12,0,9,0" /> 
           <StackPanel Width="311"> 
            <TextBlock Text="{Binding CommonName}" Foreground="#112d42" TextWrapping="NoWrap" Style="{StaticResource PhoneTextTitle2Style}"/> 
            <TextBlock Text="{Binding ScientificName}" Foreground="#112d42" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
           </StackPanel> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 

      </Grid> 
     </controls:PivotItem> 


    </controls:Pivot> 
</Grid> 

而後面的代碼...

private void panCorals_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     switch (panCorals.SelectedIndex) 
     { 
      case 0:  //search corals 

       break; 
      case 1:   //top corals 
       if (!App.vmCoral.IsTopDataLoaded) 
       { 
        App.vmCoral.IsTopLoading = true; 
        if (App.HasConnectivity) 
        { 
         //get corals from web 
         bw.DoWork += new DoWorkEventHandler(bw_DoWorkTopCoralsWeb); 

         if (bw.IsBusy != true) 
         { 
          bw.RunWorkerAsync(); 
         } 

        } 
        else 
        { 
         //get saved corals from device 
         bw.DoWork += new DoWorkEventHandler(bw_DoWorkTopCoralsSaved); 

         if (bw.IsBusy != true) 
         { 
          bw.RunWorkerAsync(); 
         } 
        } 
       } 
       break; 
      case 2:   //new corals 

       if (!App.vmCoral.IsNewDataLoaded) 
       { 
        App.vmCoral.IsNewLoading = true; 
        if (App.HasConnectivity) 
        { 
         //get corals from web 
         bw.DoWork += new DoWorkEventHandler(bw_DoWorkNewCoralsWeb); 

         if (bw.IsBusy != true) 
         { 
          bw.RunWorkerAsync(); 
         } 

        } 
        else 
        { 
         //get saved corals from device 
         bw.DoWork += new DoWorkEventHandler(bw_DoWorkNewCoralsSaved); 

         if (bw.IsBusy != true) 
         { 
          bw.RunWorkerAsync(); 
         } 
        } 
       } 

       break; 
      default: 

       break; 
     } 

    } 

回答

5

沒有什麼特別不妥您發佈本身的代碼。最有可能發生的情況是當您在數據透視表項之間切換時,後臺工作線程正在完成,這反過來又更新了您的列表綁定到的可觀察集合。此外,您的列表還包含圖像,如果您要綁定到網頁上的需要下載圖像的URL,也會導致性能問題。

有幾件事情要檢查:

  1. 確保您使用的是虛擬化堆棧面板,如果您的列表很長(這是默認的,但要確保你沒有任何地方改變了它)。
  2. 考慮使用來自SL工具包的LongListSelector控制,它具有比默認的列表框
  3. 退房低調圖像加載器,如果你正在你的圖片結合網頁的URL(http://blogs.msdn.com/b/delay/archive/2010/09/02/keep-a-low-profile-lowprofileimageloader-helps-the-windows-phone-7-ui-thread-stay-responsive-by-loading-images-in-the-background.aspx)更好的用戶界面和數據虛擬化支持。
  4. 在App.xaml.cs中啓用「EnableRedrawRegions」調試指示器。檢查在切換透視項時沒有任何操作會導致區域重新繪製(通過快速顯示不同顏色的部分屏幕指示)。如果是,請考慮使用BitMapCache。
  5. 如果你的異步處理正在下載一個長長的清單考慮分手的名單了在後臺線程,只有他們每個人之間的小延遲調度到UI線程小塊。
+0

只是想提及LongListSelector的想法,並給你信用。這似乎是真正幫助的事情。 – Jarrette

2

出現這種情況的一個常見的情況是,雖然你完全加載在一個單獨的線程數據,加載完成後,有用戶界面的工作很多對綁定表項到列表框。

我之前在網上發現的一個解決方案(我實際上丟失了鏈​​接的軌道)是將項目逐個添加到ObservableCollection(在您的案例SearchCorals中)並稍微延遲一點(比如50ms )。 (而不是批量添加)。

這樣做可以存儲在存儲器中的數據源並使用DispatcherTimer運行每隔50ms和添加項目SearchCorals一個接一個。

這解決了我的問題,我希望能與你的幫助。

+0

口吃,當我從搜索樞軸「頂」樞紐項目切換髮生。動畫暫停,然後加載首頁,然後我得到加載動畫,然後加載數據。一旦口吃結束,我必須再等幾秒鐘。 – Jarrette